| 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 |
$free = array( 'post', 'page' ); |
| 79 |
$options = $tcmp->options->getMetaboxPostTypes(); |
| 80 |
$screens = array(); |
| 81 |
foreach ( $options as $k => $v ) { |
| 82 |
if ( intval( $v ) > 0 ) { |
| 83 |
$screens[] = $k; |
| 84 |
} |
| 85 |
} |
| 86 |
if ( count( $screens ) > 0 ) { |
| 87 |
foreach ( $screens as $screen ) { |
| 88 |
add_meta_box( |
| 89 |
'tcmp_sectionid', |
| 90 |
$tcmp->lang->L( 'Tracking Code by Data443' ), |
| 91 |
'tcmp_ui_metabox', |
| 92 |
$screen, |
| 93 |
'side' |
| 94 |
); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
function tcmp_edit_snippet_array( $post, &$snippet, $prefix, $diff ) { |
| 99 |
global $tcmp; |
| 100 |
$post_id = $tcmp->utils->get( $post, 'ID', false ); |
| 101 |
if ( false === $post_id ) { |
| 102 |
$post_id = $tcmp->utils->get( $post, 'post_ID' ); |
| 103 |
} |
| 104 |
$post_type = $tcmp->utils->get( $post, 'post_type' ); |
| 105 |
|
| 106 |
$key_array = 'PostsOfType_' . $post_type; |
| 107 |
$key_active = $key_array . '_Active'; |
| 108 |
if ( 0 == $snippet[ $prefix . $key_active ] ) { |
| 109 |
$snippet[ $prefix . $key_array ] = array(); |
| 110 |
} |
| 111 |
$k = $prefix . $key_array; |
| 112 |
if ( $diff ) { |
| 113 |
$snippet[ $k ] = array_diff( $snippet[ $k ], array( $post_id ) ); |
| 114 |
} else { |
| 115 |
$snippet[ $k ] = array_merge( $snippet[ $k ], array( $post_id ) ); |
| 116 |
if ( in_array( -1, $snippet[ $k ] ) ) { |
| 117 |
$snippet[ $k ] = array( -1 ); |
| 118 |
} |
| 119 |
} |
| 120 |
$snippet[ $k ] = array_unique( $snippet[ $k ] ); |
| 121 |
$snippet[ $prefix . $key_active ] = ( count( $snippet[ $k ] ) > 0 ? 1 : 0 ); |
| 122 |
return $snippet; |
| 123 |
} |
| 124 |
//si aggancia a quando un post viene salvato per salvare anche gli altri dati del metabox |
| 125 |
add_action( 'save_post', 'tcmp_save_meta_box_data' ); |
| 126 |
function tcmp_save_meta_box_data( $post_id ) { |
| 127 |
global $tcmp; |
| 128 |
|
| 129 |
//in case of custom post type edit_ does not exist |
| 130 |
//if (!current_user_can('edit_'.$post_type, $post_id)) { |
| 131 |
// return; |
| 132 |
//} |
| 133 |
|
| 134 |
// If this is an autosave, our form has not been submitted, so we don't want to do anything. |
| 135 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
if ( ! isset( $_POST['tcmp_meta_box_nonce'] ) || ! isset( $_POST['post_type'] ) ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
// Verify that the nonce is valid. |
| 142 |
if ( ! wp_verify_nonce( $_POST['tcmp_meta_box_nonce'], 'tcmp_meta_box' ) ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
$args = array( |
| 147 |
'metabox' => true, |
| 148 |
'field' => 'id', |
| 149 |
); |
| 150 |
$ids = $tcmp->manager->get_codes( -1, $_POST, $args ); |
| 151 |
if ( ! is_array( $ids ) ) { |
| 152 |
$ids = array(); |
| 153 |
} |
| 154 |
|
| 155 |
$all_ids = tcmp_qs( 'tcmp_all_ids' ); |
| 156 |
if ( false === $all_ids || '' == $all_ids ) { |
| 157 |
$all_ids = array(); |
| 158 |
} else { |
| 159 |
$all_ids = explode( ',', $all_ids ); |
| 160 |
} |
| 161 |
$current_ids = tcmp_asqs( 'tcmp_ids', array() ); |
| 162 |
if ( ! is_array( $current_ids ) ) { |
| 163 |
$current_ids = array(); |
| 164 |
} |
| 165 |
|
| 166 |
if ( $ids != $current_ids ) { |
| 167 |
foreach ( $all_ids as $id ) { |
| 168 |
$id = intval( $id ); |
| 169 |
if ( $id <= 0 ) { |
| 170 |
continue; |
| 171 |
} |
| 172 |
if ( in_array( $id, $current_ids ) && in_array( $id, $ids ) ) { |
| 173 |
//selected now and already selected |
| 174 |
continue; |
| 175 |
} |
| 176 |
if ( ! in_array( $id, $current_ids ) && ! in_array( $id, $ids ) ) { |
| 177 |
//not selected now and not already selected |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
$snippet = $tcmp->manager->get( $id ); |
| 182 |
if ( null == $snippet ) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
$snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'include', true ); |
| 187 |
$snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'except', true ); |
| 188 |
if ( in_array( $id, $current_ids ) ) { |
| 189 |
$snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'include', false ); |
| 190 |
} else { |
| 191 |
$snippet = tcmp_edit_snippet_array( $_POST, $snippet, 'except', false ); |
| 192 |
} |
| 193 |
$tcmp->manager->put( $id, $snippet ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
$name = tcmp_sqs( 'tcmp_name' ); |
| 198 |
$code = stripslashes( tcmp_qs( 'tcmp_code' ) ); |
| 199 |
if ( '' != $name && '' != $code ) { |
| 200 |
$post_type = tcmp_sqs( 'post_type' ); |
| 201 |
$key_array = 'PostsOfType_' . $post_type; |
| 202 |
$key_active = $key_array . '_Active'; |
| 203 |
|
| 204 |
$snippet = array( |
| 205 |
'active' => 1, |
| 206 |
'name' => $name, |
| 207 |
'code' => $code, |
| 208 |
'trackPage' => TCMP_TRACK_PAGE_SPECIFIC, |
| 209 |
'trackMode' => TCMP_TRACK_MODE_CODE, |
| 210 |
); |
| 211 |
$snippet[ 'include' . $key_active ] = 1; |
| 212 |
$snippet[ 'include' . $key_array ] = array( $post_id ); |
| 213 |
$snippet = $tcmp->manager->put( '', $snippet ); |
| 214 |
$tcmp->log->debug( 'NEW SNIPPET REGISTRED=%s', $snippet ); |
| 215 |
} |
| 216 |
} |
| 217 |
|