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
175 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 | $snippets=$tcm->Manager->values(); |
| 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 | $postType=$post->post_type; |
| 19 | foreach($snippets as $snippet) { |
| 20 | $id=$snippet['id']; |
| 21 | $disabled=''; |
| 22 | $checked=''; |
| 23 | |
| 24 | if($snippet['active']==0) { |
| 25 | $disabled=' DISABLED'; |
| 26 | } elseif($snippet['exceptPostsOfType_'.$postType.'_Active']>0 && in_array(-1, $snippet['exceptPostsOfType_'.$postType])) { |
| 27 | //the user have excluded all the posts of this type from code definition |
| 28 | $disabled=' DISABLED'; |
| 29 | } else { |
| 30 | if(in_array($id, $ids)) { |
| 31 | $checked=' checked'; |
| 32 | $active=($snippet['includePostsOfType_'.$postType.'_Active']>0); |
| 33 | if(!$active) { |
| 34 | $checked=''; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | ?> |
| 39 | <input type="checkbox" class="tcm-checkbox" name="tcm_ids[]" value="<?php echo $id?>" <?php echo $checked ?> <?php echo $disabled ?> /> |
| 40 | <a href="<?php echo TCM_TAB_EDITOR_URI?>&id=<?php echo $id?>" target="_blank"><?php echo $snippet['name']?></a> |
| 41 | <br/> |
| 42 | <?php } ?> |
| 43 | </div> |
| 44 | |
| 45 | <br/> |
| 46 | <?php if($tcm->Manager->rc()>0) { ?> |
| 47 | <div> |
| 48 | <label for="tcm_name"><?php $tcm->Lang->P('Or add a name')?></label> |
| 49 | <br/> |
| 50 | <input type="text" name="tcm_name" value="" style="width:100%"/> |
| 51 | </div> |
| 52 | <div> |
| 53 | <label for="code"><?php $tcm->Lang->P('and paste HTML code here')?></label> |
| 54 | <br/> |
| 55 | <textarea dir="ltr" dirname="ltr" name="tcm_code" class="tcm-textarea" style="width:100%; height:175px;"></textarea> |
| 56 | </div> |
| 57 | <?php } else { ?> |
| 58 | <span style="color:red;font-weight:bold;"><?php $tcm->Lang->P('FreeLicenseReached')?></span> |
| 59 | <?php } |
| 60 | } |
| 61 | |
| 62 | //si aggancia per creare i metabox in post e page |
| 63 | add_action('add_meta_boxes', 'tcm_add_meta_box'); |
| 64 | function tcm_add_meta_box() { |
| 65 | global $tcm; |
| 66 | |
| 67 | $free=array('post', 'page'); |
| 68 | $options=$tcm->Options->getMetaboxPostTypes(); |
| 69 | $screens=array(); |
| 70 | foreach($options as $k=>$v) { |
| 71 | if(intval($v)>0) { |
| 72 | $screens[]=$k; |
| 73 | } |
| 74 | } |
| 75 | if(count($screens)>0) { |
| 76 | foreach ($screens as $screen) { |
| 77 | add_meta_box( |
| 78 | 'tcm_sectionid' |
| 79 | , $tcm->Lang->L('Tracking Code by IntellyWP') |
| 80 | , 'tcm_ui_metabox' |
| 81 | , $screen |
| 82 | , 'side' |
| 83 | ); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | //si aggancia a quando un post viene salvato per salvare anche gli altri dati del metabox |
| 88 | add_action('save_post', 'tcm_save_meta_box_data'); |
| 89 | function tcm_save_meta_box_data($postId) { |
| 90 | global $tcm; |
| 91 | |
| 92 | $postType=$_POST['post_type']; |
| 93 | //in case of custom post type edit_ does not exist |
| 94 | //if (!current_user_can('edit_'.$postType, $postId)) { |
| 95 | // return; |
| 96 | //} |
| 97 | |
| 98 | // If this is an autosave, our form has not been submitted, so we don't want to do anything. |
| 99 | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 100 | return; |
| 101 | } |
| 102 | if (!isset($_POST['tcm_meta_box_nonce'])) { |
| 103 | return; |
| 104 | } |
| 105 | // Verify that the nonce is valid. |
| 106 | if (!wp_verify_nonce( $_POST['tcm_meta_box_nonce'], 'tcm_meta_box')) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | $previousIds=explode(',', $tcm->Utils->qs('tcm_previous_ids')); |
| 111 | $currentIds=$tcm->Utils->qs('tcm_ids', array()); |
| 112 | $keyArray='PostsOfType_'.$postType; |
| 113 | $keyActive=$keyArray.'_Active'; |
| 114 | |
| 115 | if($previousIds!=$currentIds) { |
| 116 | //first remove by ids from old snippets |
| 117 | foreach($previousIds as $id) { |
| 118 | $id=intval($id); |
| 119 | if($id>0 && !in_array($id, $currentIds)) { |
| 120 | $snippet=$tcm->Manager->get($id); |
| 121 | if($snippet!=NULL) { |
| 122 | //remove my id from post type includes |
| 123 | $snippet['include'.$keyArray] = array_diff($snippet['include'.$keyArray], array($postId)); |
| 124 | $snippet['include'.$keyArray] = array_unique($snippet['include'.$keyArray]); |
| 125 | $snippet['include'.$keyActive]=(count($snippet['include'.$keyArray])>0 ? 1 : 0); |
| 126 | |
| 127 | //include it in post type exception |
| 128 | if($snippet['except'.$keyActive]==0) { |
| 129 | $snippet['except'.$keyArray]=array(); |
| 130 | } |
| 131 | $snippet['except'.$keyArray] = array_merge($snippet['except'.$keyArray], array($postId)); |
| 132 | $snippet['except'.$keyArray] = array_unique($snippet['except'.$keyArray]); |
| 133 | $snippet['except'.$keyActive]=1; |
| 134 | } |
| 135 | $tcm->Manager->put($id, $snippet); |
| 136 | } |
| 137 | } |
| 138 | //after insert by id in the snippets selected |
| 139 | foreach($currentIds as $id) { |
| 140 | $id=intval($id); |
| 141 | if($id>0 && !in_array($id, $previousIds)) { |
| 142 | $snippet = $tcm->Manager->get($id); |
| 143 | if ($snippet) { |
| 144 | //include my id in post type includes |
| 145 | if($snippet['include'.$keyActive]==0) { |
| 146 | $snippet['include'.$keyArray]=array(); |
| 147 | } |
| 148 | $snippet['include'.$keyArray] = array_merge($snippet['include'.$keyArray], array($postId)); |
| 149 | $snippet['include'.$keyArray] = array_unique($snippet['include'.$keyArray]); |
| 150 | $snippet['include'.$keyActive]=1; |
| 151 | //remove it from post type exception |
| 152 | $snippet['except'.$keyArray] = array_diff($snippet['except'.$keyArray], array($postId)); |
| 153 | $snippet['except'.$keyArray] = array_unique($snippet['except'.$keyArray]); |
| 154 | $snippet['except'.$keyActive]=(count($snippet['except'.$keyArray])>0 ? 1 : 0); |
| 155 | } |
| 156 | $tcm->Manager->put($id, $snippet); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | $name=stripslashes($tcm->Utils->qs('tcm_name')); |
| 162 | $code=$tcm->Utils->qs('tcm_code'); |
| 163 | if($name!='' && $code!='' && !$tcm->Manager->exists($name) && $tcm->Manager->rc()>0) { |
| 164 | $snippet=array( |
| 165 | 'active'=>1 |
| 166 | , 'name'=>$name |
| 167 | , 'code'=>$code |
| 168 | ); |
| 169 | $snippet['include'.$keyActive]=1; |
| 170 | $snippet['include'.$keyArray]=array($postId); |
| 171 | $snippet=$tcm->Manager->put('', $snippet); |
| 172 | $tcm->Logger->debug("NEW SNIPPET REGISTRED=%s", $snippet); |
| 173 | } |
| 174 | } |
| 175 |