PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / trunk
Tracking Code Manager vtrunk
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 1 month 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
226 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 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 TCMP_TAB_EDITOR_URI; ?>&id=<?php echo esc_attr( $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 //in case of custom post type edit_ does not exist
139 //if (!current_user_can('edit_'.$post_type, $post_id)) {
140 // return;
141 //}
142
143 // If this is an autosave, our form has not been submitted, so we don't want to do anything.
144 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
145 return;
146 }
147 if ( ! isset( $_POST['tcmp_meta_box_nonce'] ) || ! isset( $_POST['post_type'] ) ) {
148 return;
149 }
150 // Verify that the nonce is valid.
151 if ( ! wp_verify_nonce( $_POST['tcmp_meta_box_nonce'], 'tcmp_meta_box' ) ) {
152 return;
153 }
154
155 $args = array(
156 'metabox' => true,
157 'field' => 'id',
158 );
159 $ids = $tcmp->manager->get_codes( -1, $_POST, $args );
160 if ( ! is_array( $ids ) ) {
161 $ids = array();
162 }
163
164 $all_ids = tcmp_qs( 'tcmp_all_ids' );
165 if ( false === $all_ids || '' == $all_ids ) {
166 $all_ids = array();
167 } else {
168 $all_ids = explode( ',', $all_ids );
169 }
170 $current_ids = tcmp_asqs( 'tcmp_ids', array() );
171 if ( ! is_array( $current_ids ) ) {
172 $current_ids = array();
173 }
174
175 if ( $ids != $current_ids ) {
176 foreach ( $all_ids as $id ) {
177 $id = intval( $id );
178 if ( $id <= 0 ) {
179 continue;
180 }
181 if ( in_array( $id, $current_ids ) && in_array( $id, $ids ) ) {
182 //selected now and already selected
183 continue;
184 }
185 if ( ! in_array( $id, $current_ids ) && ! in_array( $id, $ids ) ) {
186 //not selected now and not already selected
187 continue;
188 }
189
190 $snippet = $tcmp->manager->get( $id );
191 if ( null == $snippet ) {
192 continue;
193 }
194
195 $snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'include', true );
196 $snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'except', true );
197 if ( in_array( $id, $current_ids ) ) {
198 $snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'include', false );
199 } else {
200 $snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'except', false );
201 }
202 $tcmp->manager->put( $id, $snippet );
203 }
204 }
205
206 $name = tcmp_sqs( 'tcmp_name' );
207 $code = stripslashes( tcmp_qs( 'tcmp_code' ) );
208 if ( '' != $name && '' != $code ) {
209 $post_type = tcmp_sqs( 'post_type' );
210 $key_array = 'PostsOfType_' . $post_type;
211 $key_active = $key_array . '_Active';
212
213 $snippet = array(
214 'active' => 1,
215 'name' => $name,
216 'code' => $code,
217 'trackPage' => TCMP_TRACK_PAGE_SPECIFIC,
218 'trackMode' => TCMP_TRACK_MODE_CODE,
219 );
220 $snippet[ 'include' . $key_active ] = 1;
221 $snippet[ 'include' . $key_array ] = array( $post_id );
222 $snippet = $tcmp->manager->put( '', $snippet );
223 $tcmp->log->debug( 'NEW SNIPPET REGISTRED=%s', $snippet );
224 }
225 }
226