PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 1.4
Tracking Code Manager v1.4
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
about.php 11 years ago editor.php 11 years ago feedback.php 11 years ago manager.php 11 years ago metabox.php 11 years ago settings.php 11 years ago
metabox.php
161 lines
1 <?php
2 function tcm_ui_metabox($post) {
3 global $tcm;
4 // Add an nonce field so we can check for it later.
5 wp_nonce_field('tcm_meta_box', 'tcm_meta_box_nonce');
6
7 $args=array('metabox'=>TRUE, 'field'=>'id');
8 $ids=$tcm->Manager->getCodes(-1, $post, $args);
9 $snippetsIds=$tcm->Manager->keys();
10 ?>
11 <div>
12 <?php $tcm->Lang->P('Select existing Tracking Code')?>..
13 </div>
14 <input type="hidden" name="tcm_previous_ids" value="<?php echo implode(',', $ids)?>" />
15
16 <div>
17 <?php
18 foreach($snippetsIds as $id) {
19 $snippet=$tcm->Manager->get($id);
20 $text=$snippet['name'];
21 $checked='';
22 if(in_array($id, $ids)) {
23 $checked=' checked';
24 $active=FALSE;
25 if($snippet['active']) {
26 $postType=$post->post_type;
27 $active=$snippet['includePostsOfType_'.$postType.'_Active']>0;
28 }
29 $text='<b style="color:'.($active ? 'green' : 'red').';">'.$text.'</b>';
30 }
31 ?>
32 <input type="checkbox" name="tcm_ids[]" value="<?php echo $id?>" <?php echo $checked ?> />
33 <a href="<?php echo TCM_TAB_EDITOR_URI?>&id=<?php echo $id?>" target="_blank"><?php echo $text?></a>
34 <br/>
35 <?php } ?>
36 </div>
37
38 <br/>
39 <?php if($tcm->Manager->rc()>0) { ?>
40 <div>
41 <label for="tcm_name">...<?php $tcm->Lang->P('or add a name')?></label>
42 <br/>
43 <input type="text" name="tcm_name" value="" style="width:100%"/>
44 </div>
45 <div>
46 <label for="code"><?php $tcm->Lang->P('and paste HTML code here')?></label>
47 <br/>
48 <textarea dir="ltr" dirname="ltr" name="tcm_code" rows="10" style="font-family:Monaco,'Courier New',Courier,monospace;font-size:12px;width:100%;color:#555;background-color: #f8f8f8;"></textarea>
49 </div>
50 <?php } else { ?>
51 <span style="color:red;font-weight:bold;"><?php $tcm->Lang->P('FreeLicenseReached')?></span>
52 <?php }
53 }
54
55 //si aggancia per creare i metabox in post e page
56 add_action('add_meta_boxes', 'tcm_add_meta_box');
57 function tcm_add_meta_box() {
58 global $tcm;
59
60 $free=array('post', 'page');
61 $options=$tcm->Options->getMetaboxPostTypes();
62 $screens=array();
63 foreach($options as $k=>$v) {
64 if(intval($v)>0) {
65 $screens[]=$k;
66 }
67 }
68 if(count($screens)>0) {
69 foreach ($screens as $screen) {
70 add_meta_box(
71 'tcm_sectionid'
72 , $tcm->Lang->L('Tracking Code by IntellyWP')
73 , 'tcm_ui_metabox'
74 , $screen
75 , 'side'
76 );
77 }
78 }
79 }
80 //si aggancia a quando un post viene salvato per salvare anche gli altri dati del metabox
81 add_action('save_post', 'tcm_save_meta_box_data');
82 function tcm_save_meta_box_data($postId) {
83 global $tcm;
84
85 $postType=$_POST['post_type'];
86 //in case of custom post type edit_ does not exist
87 //if (!current_user_can('edit_'.$postType, $postId)) {
88 // return;
89 //}
90
91 // If this is an autosave, our form has not been submitted, so we don't want to do anything.
92 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
93 return;
94 }
95 if (!isset($_POST['tcm_meta_box_nonce'])) {
96 return;
97 }
98 // Verify that the nonce is valid.
99 if (!wp_verify_nonce( $_POST['tcm_meta_box_nonce'], 'tcm_meta_box')) {
100 return;
101 }
102
103 $previousIds=explode(',', $tcm->Utils->qs('tcm_previous_ids'));
104 $currentIds=$tcm->Utils->qs('tcm_ids', array());
105 $keyArray='PostsOfType_'.$postType;
106 $keyActive=$keyArray.'_Active';
107
108 if($previousIds!=$currentIds) {
109 //first remove by ids from old snippets
110 foreach($previousIds as $id) {
111 $id=intval($id);
112 if($id>0 && !in_array($id, $currentIds)) {
113 $snippet=$tcm->Manager->get($id);
114 if($snippet!=NULL) {
115 //remove my id from post type includes
116 $snippet['include'.$keyArray] = array_diff($snippet['include'.$keyArray], array($postId));
117 $snippet['include'.$keyArray] = array_unique($snippet['include'.$keyArray]);
118 $snippet['include'.$keyActive]=(count($snippet['include'.$keyArray])>0 ? 1 : 0);
119 //include it in post type exception
120 $snippet['except'.$keyArray] = array_merge($snippet['except'.$keyArray], array($postId));
121 $snippet['except'.$keyArray] = array_unique($snippet['except'.$keyArray]);
122 $snippet['except'.$keyActive]=1;
123 }
124 $tcm->Manager->put($id, $snippet);
125 }
126 }
127 //after insert by id in the snippets selected
128 foreach($currentIds as $id) {
129 $id=intval($id);
130 if($id>0 && !in_array($id, $previousIds)) {
131 $snippet = $tcm->Manager->get($id);
132 if ($snippet) {
133 //include my id in post type includes
134 $snippet['include'.$keyArray] = array_merge($snippet['include'.$keyArray], array($postId));
135 $snippet['include'.$keyArray] = array_unique($snippet['include'.$keyArray]);
136 $snippet['include'.$keyActive]=1;
137 //remove it from post type exception
138 $snippet['except'.$keyArray] = array_diff($snippet['except'.$keyArray], array($postId));
139 $snippet['except'.$keyArray] = array_unique($snippet['except'.$keyArray]);
140 $snippet['except'.$keyActive]=(count($snippet['except'.$keyArray])>0 ? 1 : 0);
141 }
142 $tcm->Manager->put($id, $snippet);
143 }
144 }
145 }
146
147 $name=stripslashes($tcm->Utils->qs('tcm_name'));
148 $code=$tcm->Utils->qs('tcm_code');
149 if($name!='' && $code!='' && !$tcm->Manager->exists($name) && $tcm->Manager->rc()>0) {
150 $snippet=array(
151 'active'=>1
152 , 'name'=>$name
153 , 'code'=>$code
154 );
155 $snippet['include'.$keyActive]=1;
156 $snippet['include'.$keyArray]=array($postId);
157 $snippet=$tcm->Manager->put('', $snippet);
158 $tcm->Logger->debug("NEW SNIPPET REGISTRED=%s", $snippet);
159 }
160 }
161