PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 2.7.0
Tracking Code Manager v2.7.0
2.8.0 2.7.0 trunk 1.11.8 1.11.9 1.12.0 1.12.1 1.12.2 1.12.3 1.4 1.5 2.0.0 2.0.1 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0
tracking-code-manager / includes / admin / metabox.php
tracking-code-manager / includes / admin Last commit date
AdminOptions.php 3 months ago editor.php 1 month ago manager.php 1 month ago metabox.php 1 month ago settings.php 1 month ago whatsnew.php 1 month ago
metabox.php
229 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit;
3 function tcmp_ui_metabox( $post ) {
4 global $tcmp;
5 // Add an nonce field so we can check for it later.
6 wp_nonce_field( 'tcmp_meta_box', 'tcmp_meta_box_nonce' );
7
8 $args = array(
9 'metabox' => true,
10 'field' => 'id',
11 );
12 $ids = $tcmp->manager->get_codes( -1, $post, $args );
13
14 $all_ids = array();
15 $snippets = $tcmp->manager->values();
16 $post_type = $post->post_type;
17 foreach ( $snippets as $snippet ) {
18 if ( TCMP_TRACK_MODE_CODE == $snippet['trackMode'] ) {
19 if ( 0 != $snippet['active'] ) {
20 if ( 0 == $snippet[ 'exceptPostsOfType_' . $post_type . '_Active' ]
21 || ! in_array( -1, $snippet[ 'exceptPostsOfType_' . $post_type ] ) ) {
22 $all_ids[] = $snippet['id'];
23 }
24 }
25 }
26 }
27 ?>
28 <div>
29 <?php $tcmp->lang->P( 'Select existing Tracking Code' ); ?>..
30 </div>
31 <input type="hidden" name="tcmp_all_ids" value="<?php echo esc_attr( implode( ',', $all_ids ) ); ?>" />
32
33 <div>
34 <?php
35 foreach ( $snippets as $snippet ) {
36 $id = $snippet['id'];
37 if ( TCMP_TRACK_MODE_CODE != $snippet['trackMode'] ) {
38 continue;
39 }
40
41 $disabled = '';
42 $checked = '';
43
44 if ( ! in_array( $id, $all_ids ) ) {
45 $disabled = ' DISABLED';
46 } elseif ( in_array( $id, $ids ) ) {
47 $checked = ' CHECKED';
48 }
49 ?>
50 <input type="checkbox" class="tcmp-checkbox" name="tcmp_ids[]" value="<?php echo esc_attr( $id ); ?>" <?php echo esc_attr( $checked ); ?> <?php echo esc_attr( $disabled ); ?> />
51 <?php echo esc_attr( $snippet['name'] ); ?>
52 <a href="<?php echo esc_url( TCMP_TAB_EDITOR_URI . '&id=' . $id ); ?>" target="_blank">&nbsp;››</a>
53 <br/>
54 <?php } ?>
55 </div>
56
57 <br/>
58 <div>
59 <label for="tcmp_name"><?php $tcmp->lang->P( 'Or add a name' ); ?></label>
60 <br/>
61 <input type="text" name="tcmp_name" value="" style="width:100%"/>
62 </div>
63 <div>
64 <label for="code"><?php $tcmp->lang->P( 'and paste HTML code here' ); ?></label>
65 <br/>
66 <textarea dir="ltr" dirname="ltr" name="tcmp_code" class="tcmp-textarea" style="width:100%; height:175px;"></textarea>
67 </div>
68
69 <div style="clear:both"></div>
70 <i>Saving the post you'll save the tracking code</i>
71 <?php
72 }
73
74 //si aggancia per creare i metabox in post e page
75 add_action( 'add_meta_boxes', 'tcmp_add_meta_box' );
76 function tcmp_add_meta_box() {
77 global $tcmp;
78
79 if (!current_user_can('unfiltered_html')) {
80 return;
81 }
82
83 $free = array( 'post', 'page' );
84 $options = $tcmp->options->getMetaboxPostTypes();
85 $screens = array();
86 foreach ( $options as $k => $v ) {
87 if ( intval( $v ) > 0 ) {
88 $screens[] = $k;
89 }
90 }
91 if ( count( $screens ) > 0 ) {
92 foreach ( $screens as $screen ) {
93 add_meta_box(
94 'tcmp_sectionid',
95 $tcmp->lang->L( 'Tracking Code by Data443' ),
96 'tcmp_ui_metabox',
97 $screen,
98 'side'
99 );
100 }
101 }
102 }
103 function tcmp_edit_snippet_array( $post, &$snippet, $prefix, $diff ) {
104 global $tcmp;
105 $post_id = $tcmp->utils->get( $post, 'ID', false );
106 if ( false === $post_id ) {
107 $post_id = $tcmp->utils->get( $post, 'post_ID' );
108 }
109 $post_type = $tcmp->utils->get( $post, 'post_type' );
110
111 $key_array = 'PostsOfType_' . $post_type;
112 $key_active = $key_array . '_Active';
113 if ( 0 == $snippet[ $prefix . $key_active ] ) {
114 $snippet[ $prefix . $key_array ] = array();
115 }
116 $k = $prefix . $key_array;
117 if ( $diff ) {
118 $snippet[ $k ] = array_diff( $snippet[ $k ], array( $post_id ) );
119 } else {
120 $snippet[ $k ] = array_merge( $snippet[ $k ], array( $post_id ) );
121 if ( in_array( -1, $snippet[ $k ] ) ) {
122 $snippet[ $k ] = array( -1 );
123 }
124 }
125 $snippet[ $k ] = array_unique( $snippet[ $k ] );
126 $snippet[ $prefix . $key_active ] = ( count( $snippet[ $k ] ) > 0 ? 1 : 0 );
127 return $snippet;
128 }
129 //si aggancia a quando un post viene salvato per salvare anche gli altri dati del metabox
130 add_action( 'save_post', 'tcmp_save_meta_box_data' );
131 function tcmp_save_meta_box_data( $post_id ) {
132 global $tcmp;
133
134 if (!current_user_can('unfiltered_html')) {
135 return;
136 }
137
138 // If this is an autosave, our form has not been submitted, so we don't want to do anything.
139 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
140 return;
141 }
142 if ( ! isset( $_POST['tcmp_meta_box_nonce'] ) || ! isset( $_POST['post_type'] ) ) {
143 return;
144 }
145 // Verify that the nonce is valid.
146 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['tcmp_meta_box_nonce'] ) ), 'tcmp_meta_box' ) ) {
147 return;
148 }
149
150 // Object-level authorization: only proceed if the current user can actually
151 // edit this specific post. The 'edit_post' meta capability is mapped by
152 // WordPress to the correct primitive capability for the post's type, so it
153 // works for custom post types too (see F-03).
154 if ( ! current_user_can( 'edit_post', $post_id ) ) {
155 return;
156 }
157
158 $args = array(
159 'metabox' => true,
160 'field' => 'id',
161 );
162 $ids = $tcmp->manager->get_codes( -1, $_POST, $args );
163 if ( ! is_array( $ids ) ) {
164 $ids = array();
165 }
166
167 $all_ids = tcmp_qs( 'tcmp_all_ids' );
168 if ( false === $all_ids || '' == $all_ids ) {
169 $all_ids = array();
170 } else {
171 $all_ids = explode( ',', $all_ids );
172 }
173 $current_ids = tcmp_asqs( 'tcmp_ids', array() );
174 if ( ! is_array( $current_ids ) ) {
175 $current_ids = array();
176 }
177
178 if ( $ids != $current_ids ) {
179 foreach ( $all_ids as $id ) {
180 $id = intval( $id );
181 if ( $id <= 0 ) {
182 continue;
183 }
184 if ( in_array( $id, $current_ids ) && in_array( $id, $ids ) ) {
185 //selected now and already selected
186 continue;
187 }
188 if ( ! in_array( $id, $current_ids ) && ! in_array( $id, $ids ) ) {
189 //not selected now and not already selected
190 continue;
191 }
192
193 $snippet = $tcmp->manager->get( $id );
194 if ( null == $snippet ) {
195 continue;
196 }
197
198 $snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'include', true );
199 $snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'except', true );
200 if ( in_array( $id, $current_ids ) ) {
201 $snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'include', false );
202 } else {
203 $snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'except', false );
204 }
205 $tcmp->manager->put( $id, $snippet );
206 }
207 }
208
209 $name = tcmp_sqs( 'tcmp_name' );
210 $code = stripslashes( tcmp_qs( 'tcmp_code' ) );
211 if ( '' != $name && '' != $code ) {
212 $post_type = tcmp_sqs( 'post_type' );
213 $key_array = 'PostsOfType_' . $post_type;
214 $key_active = $key_array . '_Active';
215
216 $snippet = array(
217 'active' => 1,
218 'name' => $name,
219 'code' => $code,
220 'trackPage' => TCMP_TRACK_PAGE_SPECIFIC,
221 'trackMode' => TCMP_TRACK_MODE_CODE,
222 );
223 $snippet[ 'include' . $key_active ] = 1;
224 $snippet[ 'include' . $key_array ] = array( $post_id );
225 $snippet = $tcmp->manager->put( '', $snippet );
226 $tcmp->log->debug( 'NEW SNIPPET REGISTRED=%s', $snippet );
227 }
228 }
229