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