editor.php
5 years ago
manager.php
5 years ago
metabox.php
5 years ago
settings.php
5 years ago
whatsnew.php
5 years ago
metabox.php
210 lines
| 1 | <?php |
| 2 | function tcmp_ui_metabox($post) { |
| 3 | global $tcmp; |
| 4 | // Add an nonce field so we can check for it later. |
| 5 | wp_nonce_field('tcmp_meta_box', 'tcmp_meta_box_nonce'); |
| 6 | |
| 7 | $args=array('metabox'=>TRUE, 'field'=>'id'); |
| 8 | $ids=$tcmp->Manager->getCodes(-1, $post, $args); |
| 9 | |
| 10 | $allIds=array(); |
| 11 | $snippets=$tcmp->Manager->values(); |
| 12 | $postType=$post->post_type; |
| 13 | foreach($snippets as $snippet) { |
| 14 | if($snippet['trackMode']==TCMP_TRACK_MODE_CODE) { |
| 15 | if($snippet['active']!=0) { |
| 16 | if($snippet['exceptPostsOfType_'.$postType.'_Active']==0 |
| 17 | || !in_array(-1, $snippet['exceptPostsOfType_'.$postType])) { |
| 18 | $allIds[]=$snippet['id']; |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | ?> |
| 24 | <div> |
| 25 | <?php $tcmp->Lang->P('Select existing Tracking Code')?>.. |
| 26 | </div> |
| 27 | <input type="hidden" name="tcmp_all_ids" value="<?php echo implode(',', $allIds)?>" /> |
| 28 | |
| 29 | <div> |
| 30 | <?php |
| 31 | foreach($snippets as $snippet) { |
| 32 | $id=$snippet['id']; |
| 33 | if($snippet['trackMode']!=TCMP_TRACK_MODE_CODE) { |
| 34 | continue; |
| 35 | } |
| 36 | |
| 37 | $disabled=''; |
| 38 | $checked=''; |
| 39 | |
| 40 | if(!in_array($id, $allIds)) { |
| 41 | $disabled=' DISABLED'; |
| 42 | } elseif(in_array($id, $ids)) { |
| 43 | $checked=' CHECKED'; |
| 44 | } |
| 45 | ?> |
| 46 | <input type="checkbox" class="tcmp-checkbox" name="tcmp_ids[]" value="<?php echo $id?>" <?php echo $checked ?> <?php echo $disabled ?> /> |
| 47 | <?php echo $snippet['name']?> |
| 48 | <a href="<?php echo TCMP_TAB_EDITOR_URI?>&id=<?php echo $id?>" target="_blank"> ››</a> |
| 49 | <br/> |
| 50 | <?php } ?> |
| 51 | </div> |
| 52 | |
| 53 | <br/> |
| 54 | <div> |
| 55 | <label for="tcmp_name"><?php $tcmp->Lang->P('Or add a name')?></label> |
| 56 | <br/> |
| 57 | <input type="text" name="tcmp_name" value="" style="width:100%"/> |
| 58 | </div> |
| 59 | <div> |
| 60 | <label for="code"><?php $tcmp->Lang->P('and paste HTML code here')?></label> |
| 61 | <br/> |
| 62 | <textarea dir="ltr" dirname="ltr" name="tcmp_code" class="tcmp-textarea" style="width:100%; height:175px;"></textarea> |
| 63 | </div> |
| 64 | |
| 65 | <div style="clear:both"></div> |
| 66 | <i>Saving the post you'll save the tracking code</i> |
| 67 | <?php } |
| 68 | |
| 69 | //si aggancia per creare i metabox in post e page |
| 70 | add_action('add_meta_boxes', 'tcmp_add_meta_box'); |
| 71 | function tcmp_add_meta_box() { |
| 72 | global $tcmp; |
| 73 | |
| 74 | $free=array('post', 'page'); |
| 75 | $options=$tcmp->Options->getMetaboxPostTypes(); |
| 76 | $screens=array(); |
| 77 | foreach($options as $k=>$v) { |
| 78 | if(intval($v)>0) { |
| 79 | $screens[]=$k; |
| 80 | } |
| 81 | } |
| 82 | if(count($screens)>0) { |
| 83 | foreach ($screens as $screen) { |
| 84 | add_meta_box( |
| 85 | 'tcmp_sectionid' |
| 86 | , $tcmp->Lang->L('Tracking Code PRO by IntellyWP') |
| 87 | , 'tcmp_ui_metabox' |
| 88 | , $screen |
| 89 | , 'side' |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | function tcmp_edit_snippet_array($post, &$snippet, $prefix, $diff) { |
| 95 | global $tcmp; |
| 96 | $postId=$tcmp->Utils->get($post, 'ID', FALSE); |
| 97 | if($postId===FALSE) { |
| 98 | $postId=$tcmp->Utils->get($post, 'post_ID'); |
| 99 | } |
| 100 | $postType=$tcmp->Utils->get($post, 'post_type'); |
| 101 | |
| 102 | $keyArray='PostsOfType_'.$postType; |
| 103 | $keyActive=$keyArray.'_Active'; |
| 104 | if($snippet[$prefix.$keyActive]==0) { |
| 105 | $snippet[$prefix.$keyArray]=array(); |
| 106 | } |
| 107 | $k=$prefix.$keyArray; |
| 108 | if($diff) { |
| 109 | $snippet[$k]=array_diff($snippet[$k], array($postId)); |
| 110 | } else { |
| 111 | $snippet[$k]=array_merge($snippet[$k], array($postId)); |
| 112 | if(in_array(-1, $snippet[$k])) { |
| 113 | $snippet[$k]=array(-1); |
| 114 | } |
| 115 | } |
| 116 | $snippet[$k]=array_unique($snippet[$k]); |
| 117 | $snippet[$prefix.$keyActive]=(count($snippet[$k])>0 ? 1 : 0); |
| 118 | return $snippet; |
| 119 | } |
| 120 | //si aggancia a quando un post viene salvato per salvare anche gli altri dati del metabox |
| 121 | add_action('save_post', 'tcmp_save_meta_box_data'); |
| 122 | function tcmp_save_meta_box_data($postId) { |
| 123 | global $tcmp; |
| 124 | |
| 125 | //in case of custom post type edit_ does not exist |
| 126 | //if (!current_user_can('edit_'.$postType, $postId)) { |
| 127 | // return; |
| 128 | //} |
| 129 | |
| 130 | // If this is an autosave, our form has not been submitted, so we don't want to do anything. |
| 131 | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 132 | return; |
| 133 | } |
| 134 | if (!isset($_POST['tcmp_meta_box_nonce']) || !isset($_POST['post_type'])) { |
| 135 | return; |
| 136 | } |
| 137 | // Verify that the nonce is valid. |
| 138 | if (!wp_verify_nonce($_POST['tcmp_meta_box_nonce'], 'tcmp_meta_box')) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | $args=array('metabox'=>TRUE, 'field'=>'id'); |
| 143 | $ids=$tcmp->Manager->getCodes(-1, $_POST, $args); |
| 144 | if(!is_array($ids)) { |
| 145 | $ids=array(); |
| 146 | } |
| 147 | |
| 148 | $allIds=TCMP_QS('tcmp_all_ids'); |
| 149 | if($allIds===FALSE || $allIds=='') { |
| 150 | $allIds=array(); |
| 151 | } else { |
| 152 | $allIds=explode(',', $allIds); |
| 153 | } |
| 154 | $currentIds=TCMP_ASQS('tcmp_ids', array()); |
| 155 | if(!is_array($currentIds)) { |
| 156 | $currentIds=array(); |
| 157 | } |
| 158 | |
| 159 | if($ids!=$currentIds) { |
| 160 | foreach($allIds as $id) { |
| 161 | $id=intval($id); |
| 162 | if($id<=0) { |
| 163 | continue; |
| 164 | } |
| 165 | if(in_array($id, $currentIds) && in_array($id, $ids)) { |
| 166 | //selected now and already selected |
| 167 | continue; |
| 168 | } |
| 169 | if(!in_array($id, $currentIds) && !in_array($id, $ids)) { |
| 170 | //not selected now and not already selected |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | $snippet=$tcmp->Manager->get($id); |
| 175 | if($snippet==NULL) { |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | $snippet=tcmp_edit_snippet_array($_POST, $snippet, 'include', TRUE); |
| 180 | $snippet=tcmp_edit_snippet_array($_POST, $snippet, 'except', TRUE); |
| 181 | if(in_array($id, $currentIds)) { |
| 182 | $snippet=tcmp_edit_snippet_array($_POST, $snippet, 'include', FALSE); |
| 183 | } else { |
| 184 | $snippet=tcmp_edit_snippet_array($_POST, $snippet, 'except', FALSE); |
| 185 | } |
| 186 | $tcmp->Manager->put($id, $snippet); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | $name=TCMP_SQS('tcmp_name'); |
| 191 | $code=stripslashes(TCMP_QS('tcmp_code')); |
| 192 | if($name!='' && $code!='') { |
| 193 | $postType=TCMP_SQS('post_type'); |
| 194 | $keyArray='PostsOfType_'.$postType; |
| 195 | $keyActive=$keyArray.'_Active'; |
| 196 | |
| 197 | $snippet=array( |
| 198 | 'active'=>1 |
| 199 | , 'name'=>$name |
| 200 | , 'code'=>$code |
| 201 | , 'trackPage'=>TCMP_TRACK_PAGE_SPECIFIC |
| 202 | , 'trackMode'=>TCMP_TRACK_MODE_CODE |
| 203 | ); |
| 204 | $snippet['include'.$keyActive]=1; |
| 205 | $snippet['include'.$keyArray]=array($postId); |
| 206 | $snippet=$tcmp->Manager->put('', $snippet); |
| 207 | $tcmp->Log->debug("NEW SNIPPET REGISTRED=%s", $snippet); |
| 208 | } |
| 209 | } |
| 210 |