| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
class WINP_SnippetsViewTable extends Wbcr_FactoryViewtables410_Viewtable { |
| 9 |
|
| 10 |
public function configure() { |
| 11 |
/** |
| 12 |
* Columns |
| 13 |
*/ |
| 14 |
$this->columns->clear(); |
| 15 |
//$this->columns->add('status', __('Status', 'insert-php')); |
| 16 |
$this->columns->add( 'winp_actions', __( 'Status', 'insert-php' ) ); |
| 17 |
$this->columns->add( 'title', __( 'Snippet title', 'insert-php' ) ); |
| 18 |
$this->columns->add( 'winp_description', __( 'Description', 'insert-php' ) ); |
| 19 |
$this->columns->add( 'winp_where_use', __( 'Where use?', 'insert-php' ) ); |
| 20 |
$this->columns->add( 'winp_taxonomy', __( 'Tags', 'insert-php' ) ); |
| 21 |
$this->columns->add( 'winp_priority', __( 'Priority', 'insert-php' ) ); |
| 22 |
$this->columns->add( 'winp_snippet_type', '' ); |
| 23 |
|
| 24 |
/** |
| 25 |
* Scripts & styles |
| 26 |
*/ |
| 27 |
$this->styles->add( WINP_PLUGIN_URL . '/admin/assets/css/list-table.css' ); |
| 28 |
$this->scripts->add( WINP_PLUGIN_URL . '/admin/assets/js/snippet-list.js' ); |
| 29 |
$this->scripts->localize( 'winp_ajax', [ 'nonce' => wp_create_nonce( 'winp_ajax' ) ] ); |
| 30 |
$this->runActions(); |
| 31 |
|
| 32 |
add_filter( 'manage_edit-' . WINP_SNIPPETS_POST_TYPE . '_sortable_columns', array( |
| 33 |
$this, |
| 34 |
'sortable_columns' |
| 35 |
) ); |
| 36 |
add_action( 'wp_ajax_change_priority', [ $this, 'change_priority' ] ); |
| 37 |
add_action( 'wp_ajax_change_snippet_status', [ $this, 'change_snippet_status' ] ); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Column 'Type' |
| 43 |
* |
| 44 |
* @param $post |
| 45 |
*/ |
| 46 |
public function columnWinp_snippet_type( $post ) { |
| 47 |
$type = WINP_Helper::getMetaOption( $post->ID, 'snippet_type', WINP_SNIPPET_TYPE_PHP ); |
| 48 |
$class = 'wbcr-inp-type-' . esc_attr( $type ); |
| 49 |
$type = $type == 'universal' ? 'uni' : $type; |
| 50 |
$type = $type == 'advert' ? 'ad' : $type; |
| 51 |
|
| 52 |
echo '<div class="wbcr-inp-snippet-type-label ' . esc_attr( $class ) . '">' . esc_html( $type ) . '</div>'; |
| 53 |
} |
| 54 |
|
| 55 |
public function columnWinp_description( $post ) { |
| 56 |
echo esc_html( WINP_Helper::getMetaOption( $post->ID, 'snippet_description' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Column 'Where_use' |
| 61 |
* |
| 62 |
* @param $post |
| 63 |
*/ |
| 64 |
public function columnWinp_where_use( $post ) { |
| 65 |
$click = ""; |
| 66 |
$snippet_scope = WINP_Helper::getMetaOption( $post->ID, 'snippet_scope' ); |
| 67 |
if ( $snippet_scope == 'shortcode' ) { |
| 68 |
$click = "onclick='this.setSelectionRange(0, this.value.length)'"; |
| 69 |
} |
| 70 |
|
| 71 |
$value = WINP_Helper::get_where_use_text( $post ); |
| 72 |
echo "<input type='text' name='wbcr_inp_shortcode_input' class='wbcr_inp_shortcode_input' value='{$value}' readonly='readonly' {$click}>"; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Column 'Priority' |
| 77 |
* |
| 78 |
* @param $post |
| 79 |
* |
| 80 |
* @since 2.4.0 |
| 81 |
* |
| 82 |
*/ |
| 83 |
public function columnWinp_taxonomy( $post ) { |
| 84 |
$post_cat = get_the_terms( $post->ID, WINP_SNIPPETS_TAXONOMY ); |
| 85 |
$result = []; |
| 86 |
if ( is_array( $post_cat ) ) { |
| 87 |
foreach ( $post_cat as $item ) { |
| 88 |
$href = admin_url( "edit.php?post_type=" . WINP_SNIPPETS_POST_TYPE . "&winp_filter_tag={$item->slug}" ); |
| 89 |
$result[] = "<a href='{$href}' class='winp-taxonomy-href'>{$item->name}</a>"; |
| 90 |
} |
| 91 |
} |
| 92 |
echo implode( ', ', $result ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Column 'Priority' |
| 97 |
* |
| 98 |
* @param $post |
| 99 |
* |
| 100 |
* @since 2.4.0 |
| 101 |
* |
| 102 |
*/ |
| 103 |
public function columnWinp_priority( $post ) { |
| 104 |
$snippet_priority = WINP_Helper::getMetaOption( $post->ID, 'snippet_priority' ); |
| 105 |
echo "<input type='number' name='wbcr_inp_input_priority' class='wbcr_inp_input_priority' |
| 106 |
data-snippet-id='{$post->ID}' value='{$snippet_priority}'>"; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Column 'Actions' |
| 111 |
* |
| 112 |
* @param $post |
| 113 |
*/ |
| 114 |
public function columnWinp_actions( $post ) { |
| 115 |
$post_id = (int) $post->ID; |
| 116 |
$is_activate = (int) WINP_Helper::getMetaOption( $post_id, 'snippet_activate', 0 ); |
| 117 |
$css_class = 'winp-inactive'; |
| 118 |
|
| 119 |
if ( $is_activate ) { |
| 120 |
$css_class = ''; |
| 121 |
} |
| 122 |
|
| 123 |
echo "<a class='winp-snippet-active-switch {$css_class}' id='winp-snippet-status-switch' data-snippet-id='{$post_id}' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&post=' . $post_id . '&action=wbcr_inp_activate_snippet' ), 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) . "'> </a>"; |
| 124 |
//echo '<a class="wbcr-inp-enable-snippet-button button" href="' . wp_nonce_url( admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&post=' . $post_id . '&action=wbcr_inp_activate_snippet' ), 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) . '"><span class="dashicons ' . esc_attr( $icon ) . '"></span></a>'; |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
* Activate/Deactivate snippet |
| 129 |
*/ |
| 130 |
protected function runActions() { |
| 131 |
if ( WINP_Plugin::app()->request->get( 'post_type', '', true ) == WINP_SNIPPETS_POST_TYPE ) { |
| 132 |
$post = WINP_Plugin::app()->request->get( 'post', 0 ); |
| 133 |
$action = WINP_Plugin::app()->request->get( 'action', '', 'sanitize_key' ); |
| 134 |
|
| 135 |
if ( ! empty( $action ) && ! empty( $post ) && 'wbcr_inp_activate_snippet' == $action ) { |
| 136 |
$post_id = (int) $post; |
| 137 |
$wpnonce = WINP_Plugin::app()->request->get( '_wpnonce', '' ); |
| 138 |
|
| 139 |
if ( ! wp_verify_nonce( $wpnonce, 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) || ! WINP_Plugin::app()->currentUserCan() ) { |
| 140 |
wp_die( 'Permission error. You can not edit this page.' ); |
| 141 |
} |
| 142 |
|
| 143 |
$is_activate = (int) WINP_Helper::getMetaOption( $post_id, 'snippet_activate', 0 ); |
| 144 |
$snippet_scope = WINP_Helper::getMetaOption( $post_id, 'snippet_scope' ); |
| 145 |
$snippet_type = WINP_Helper::get_snippet_type( $post_id ); |
| 146 |
|
| 147 |
/** |
| 148 |
* Prevent activation of the snippet if it contains an error. This will not allow the user to break his site. |
| 149 |
* |
| 150 |
* @since 2.0.5 |
| 151 |
*/ |
| 152 |
if ( ( 'evrywhere' == $snippet_scope || 'auto' == $snippet_scope ) && $snippet_type != WINP_SNIPPET_TYPE_TEXT && $snippet_type != WINP_SNIPPET_TYPE_AD && $snippet_type != WINP_SNIPPET_TYPE_CSS && $snippet_type != WINP_SNIPPET_TYPE_JS && ! $is_activate ) { |
| 153 |
if ( WINP_Plugin::app()->getExecuteObject()->getSnippetError( $post_id ) ) { |
| 154 |
wp_safe_redirect( add_query_arg( [ |
| 155 |
'action' => 'edit', |
| 156 |
'post' => $post_id, |
| 157 |
'wbcr_inp_save_snippet_result' => 'code-error', |
| 158 |
], admin_url( 'post.php' ) ) ); |
| 159 |
exit; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$status = ! $is_activate; |
| 164 |
|
| 165 |
update_post_meta( $post_id, $this->plugin->getPrefix() . 'snippet_activate', $status ); |
| 166 |
|
| 167 |
$redirect_url = add_query_arg( [ |
| 168 |
'post_type' => WINP_SNIPPETS_POST_TYPE, |
| 169 |
'wbcr_inp_snippet_updated' => 1, |
| 170 |
], admin_url( 'edit.php' ) ); |
| 171 |
|
| 172 |
wp_safe_redirect( $redirect_url ); |
| 173 |
exit; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @param $sortable_columns |
| 180 |
*/ |
| 181 |
public function sortable_columns( $sortable_columns ) { |
| 182 |
$sortable_columns['winp_priority'] = 'winp_priority'; |
| 183 |
|
| 184 |
return $sortable_columns; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* AJAX action for change priority |
| 189 |
*/ |
| 190 |
public function change_priority() { |
| 191 |
check_ajax_referer( 'winp_ajax' ); |
| 192 |
|
| 193 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 194 |
wp_send_json( array( 'error_message' => __( "You don't have enough capability to edit this information.", 'insert-php' ) ) ); |
| 195 |
} |
| 196 |
|
| 197 |
if ( isset( $_POST['snippet_id'] ) && isset( $_POST['priority'] ) ) { |
| 198 |
if ( is_numeric( $_POST['priority'] ) ) { |
| 199 |
WINP_Helper::updateMetaOption( $_POST['snippet_id'], 'snippet_priority', $_POST['priority'] ); |
| 200 |
|
| 201 |
wp_send_json( [ |
| 202 |
'message' => __( "Priority successfully changed", "insert-php" ), |
| 203 |
] ); |
| 204 |
} else { |
| 205 |
wp_send_json( [ |
| 206 |
'error_message' => __( "Priority is not changed! It's must be a number", 'insert-php' ), |
| 207 |
] ); |
| 208 |
} |
| 209 |
|
| 210 |
} else { |
| 211 |
wp_send_json( [ |
| 212 |
'error_message' => __( 'Priority is not changed!', 'insert-php' ), |
| 213 |
] ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* AJAX action for change priority |
| 219 |
*/ |
| 220 |
public function change_snippet_status() { |
| 221 |
check_ajax_referer( 'winp_ajax' ); |
| 222 |
|
| 223 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 224 |
wp_send_json( array( 'error_message' => __( "You don't have enough capability to edit this information.", 'insert-php' ) ) ); |
| 225 |
} |
| 226 |
|
| 227 |
if ( isset( $_POST['snippet_id'] ) ) { |
| 228 |
$snippet_id = $_POST['snippet_id']; |
| 229 |
$is_activate = (int) WINP_Helper::getMetaOption( $snippet_id, 'snippet_activate', 0 ); |
| 230 |
$snippet_scope = WINP_Helper::getMetaOption( $snippet_id, 'snippet_scope' ); |
| 231 |
$snippet_type = WINP_Helper::get_snippet_type( $snippet_id ); |
| 232 |
|
| 233 |
/** |
| 234 |
* Prevent activation of the snippet if it contains an error. This will not allow the user to break his site. |
| 235 |
* |
| 236 |
* @since 2.0.5 |
| 237 |
*/ |
| 238 |
if ( ( 'evrywhere' == $snippet_scope || 'auto' == $snippet_scope ) && $snippet_type != WINP_SNIPPET_TYPE_TEXT && $snippet_type != WINP_SNIPPET_TYPE_AD && $snippet_type != WINP_SNIPPET_TYPE_CSS && $snippet_type != WINP_SNIPPET_TYPE_JS && ! $is_activate ) { |
| 239 |
if ( WINP_Plugin::app()->getExecuteObject()->getSnippetError( $snippet_id ) ) { |
| 240 |
wp_send_json( [ |
| 241 |
'alert' => true, |
| 242 |
'error_message' => __( "The snippet is not activated because errors were detected in the snippet code!", 'insert-php' ), |
| 243 |
] ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
$status = ! $is_activate; |
| 248 |
|
| 249 |
$ok = update_post_meta( $snippet_id, $this->plugin->getPrefix() . 'snippet_activate', $status ); |
| 250 |
|
| 251 |
if ( $ok ) { |
| 252 |
wp_send_json( [ |
| 253 |
'message' => __( "Snippet status changed", "insert-php" ), |
| 254 |
] ); |
| 255 |
} else { |
| 256 |
wp_send_json( [ |
| 257 |
'error_message' => __( 'Snippet status not changed.', 'insert-php' ), |
| 258 |
] ); |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
} else { |
| 263 |
wp_send_json( [ |
| 264 |
'error_message' => __( 'Snippet status not changed. No snippet ID', 'insert-php' ), |
| 265 |
] ); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|