-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-paramscraper.php
176 lines (155 loc) · 3.78 KB
/
wp-paramscraper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Plugin Name: WP Paramscaper
* Description: Helps to collect and pass tracking params
* Version: 1.0.0
*
* Text Domain: wp-paramscraper
*
* @package WpParamscraper
*/
/**
* WpParamscraper Class
*/
class WpParamscraper {
/**
* Construct the plugin
*/
public function __construct() {
/**
* Add settings menu item
*/
add_action( 'init', array( $this, 'set_params' ) );
add_action( 'admin_menu', array( $this, 'create_menu_item' ) );
add_action( 'wp_ajax_new_param', array( $this, 'get_field' ) );
add_shortcode( 'get_param', array( $this, 'get_params' ) );
}
/**
* Paramscraper menu item function
*/
public function create_menu_item() {
add_submenu_page( 'options-general.php', 'Paramscaper', 'Paramscaper', 'manage_options', 'paramscraper', array( $this, 'paramscraper_settings' ) );
return;
}
/**
* Paramscraper page function
*/
public function paramscraper_settings() {
?>
<div class="wrap">
<form method="post" action="options.php">
<?php
wp_nonce_field( 'update-options' );
?>
<script>
jQuery(function ($) {
$('body').on('click', '.add-param', function (e) {
e.preventDefault();
$.ajax({
url: ajaxurl,
type: 'post',
data: {
action: 'new_param'
},
success: function( data ) {
$('.all-params').append(data);
}
});
}).on('click', '.remove-param', function (e) {
e.preventDefault();
$(this).parent().remove();
});
});
</script>
<button class="add-param">Add new param</button>
<div class="all-params">
<?php
foreach ( get_option( 'paramscraper-input' ) as $param ) {
echo self::render_input( $param );
}
?>
</div>
<input type="submit" name="submit" value="Save"></input>
<input name="action" value="update" type="hidden"></input>
<input name="page_options" value="paramscraper-input" type="hidden"></input>
</form>
</div>
<?php
return;
}
/**
* Get and echo input
*/
public function get_field() {
echo self::render_input();
wp_die();
}
/**
* Input field
*
* @param string $val value.
* @return string
*/
public function render_input( $val ) {
return <<<HTML
<div class="param-container">
<label>
<input type="text" id="paramscraper-input" name="paramscraper-input[]" value="{$val}">
</label>
<button class="remove-param">Remove</button>
</div>
HTML;
}
/**
* Set params based on inputs
*/
public function set_params() {
if ( ! session_id() ) {
session_start();
}
$params = [];
foreach ( get_option( 'paramscraper-input' ) as $param ) {
if ( isset( $_GET[$param] ) && ! empty( $_GET[$param] ) ) {
$params[$param] = $_GET[$param];
}
}
if ( count( $params ) ) {
$_SESSION['paramscraper_params'] = $params;
}
}
/**
* Get param from session using shortcode
*
* @param string $atts [description].
* @return string [description]
*/
public static function get_param( $atts ) {
$atts = shortcode_atts( array(
'param' => $param,
), $atts, 'get_param' );
if ( isset( $_SESSION['paramscraper_params'][$atts['param']] ) ) {
return $_SESSION['paramscraper_params'][$atts['param']];
}
}
/**
* Get params from session
*
* @return array array of params
*/
public static function get_params() {
if ( ! session_id() ) {
session_start();
}
if ( ! isset( $_SESSION['paramscraper_params'] ) ) {
return [];
}
$params = [];
foreach ( get_option( 'paramscraper-input' ) as $param ) {
if ( isset( $_SESSION['paramscraper_params'][$param] ) && ! empty( $_SESSION['paramscraper_params'][$param] ) ) {
$params[$param] = $_SESSION['paramscraper_params'][$param];
}
}
return $params;
}
}
$wp_paramscraper = new WpParamscraper();