| @@ -1,180 +1,426 @@ | ||
| 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( 'title', __( 'Snippet title', 'insert-php' ) ); | |
| 17 | - $this->columns->add( 'winp_description', __( 'Description', 'insert-php' ) ); | |
| 18 | - $this->columns->add( 'winp_actions', __( 'Status', 'insert-php' ) ); | |
| 19 | - $this->columns->add( 'winp_where_use', __( 'Where use?', 'insert-php' ) ); | |
| 20 | - $this->columns->add( 'winp_taxonomy-' . WINP_SNIPPETS_TAXONOMY, __( 'Tags', 'insert-php' ) ); | |
| 21 | - $this->columns->add( 'winp_snippet_type', '' ); | |
| 22 | - | |
| 23 | - /** | |
| 24 | - * Scripts & styles | |
| 25 | - */ | |
| 26 | - $this->styles->add( WINP_PLUGIN_URL . '/admin/assets/css/list-table.css' ); | |
| 27 | - $this->runActions(); | |
| 28 | - } | |
| 29 | - | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * Column 'Type' | |
| 33 | - * | |
| 34 | - * @param $post | |
| 35 | - */ | |
| 36 | - public function columnWinp_snippet_type( $post ) { | |
| 37 | - $type = WINP_Helper::getMetaOption( $post->ID, 'snippet_type', WINP_SNIPPET_TYPE_PHP ); | |
| 38 | - $class = 'wbcr-inp-type-' . esc_attr( $type ); | |
| 39 | - $type = $type == 'universal' ? 'uni' : $type; | |
| 40 | - | |
| 41 | - echo '<div class="wbcr-inp-snippet-type-label ' . esc_attr( $class ) . '">' . esc_html( $type ) . '</div>'; | |
| 42 | - } | |
| 43 | - | |
| 44 | - public function columnWinp_description( $post ) { | |
| 45 | - echo esc_html( WINP_Helper::getMetaOption( $post->ID, 'snippet_description' ) ); | |
| 46 | - } | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * Column 'Where_use' | |
| 50 | - * | |
| 51 | - * @param $post | |
| 52 | - */ | |
| 53 | - public function columnWinp_where_use( $post ) { | |
| 54 | - $snippet_scope = WINP_Helper::getMetaOption( $post->ID, 'snippet_scope' ); | |
| 55 | - | |
| 56 | - if ( $snippet_scope == 'evrywhere' ) { | |
| 57 | - echo __( 'Run everywhere', 'insert-php' ); | |
| 58 | - } else if ( $snippet_scope == 'auto' ) { | |
| 59 | - $items = [ | |
| 60 | - WINP_SNIPPET_AUTO_HEADER => __( 'Header', 'insert-php' ), | |
| 61 | - WINP_SNIPPET_AUTO_FOOTER => __( 'Footer', 'insert-php' ), | |
| 62 | - WINP_SNIPPET_AUTO_BEFORE_POST => __( 'Insert Before Post', 'insert-php' ), | |
| 63 | - WINP_SNIPPET_AUTO_BEFORE_CONTENT => __( 'Insert Before Content', 'insert-php' ), | |
| 64 | - WINP_SNIPPET_AUTO_BEFORE_PARAGRAPH => __( 'Insert Before Paragraph', 'insert-php' ), | |
| 65 | - WINP_SNIPPET_AUTO_AFTER_PARAGRAPH => __( 'Insert After Paragraph', 'insert-php' ), | |
| 66 | - WINP_SNIPPET_AUTO_AFTER_CONTENT => __( 'Insert After Content', 'insert-php' ), | |
| 67 | - WINP_SNIPPET_AUTO_AFTER_POST => __( 'Insert After Post', 'insert-php' ), | |
| 68 | - WINP_SNIPPET_AUTO_BEFORE_EXCERPT => __( 'Insert Before Excerpt', 'insert-php' ), | |
| 69 | - WINP_SNIPPET_AUTO_AFTER_EXCERPT => __( 'Insert After Excerpt', 'insert-php' ), | |
| 70 | - WINP_SNIPPET_AUTO_BETWEEN_POSTS => __( 'Between Posts', 'insert-php' ), | |
| 71 | - WINP_SNIPPET_AUTO_BEFORE_POSTS => __( 'Before post', 'insert-php' ), | |
| 72 | - WINP_SNIPPET_AUTO_AFTER_POSTS => __( 'After post', 'insert-php' ), | |
| 73 | - ]; | |
| 74 | - | |
| 75 | - $snippet_location = WINP_Helper::getMetaOption( $post->ID, 'snippet_location', '' ); | |
| 76 | - | |
| 77 | - switch ( $snippet_location ) { | |
| 78 | - case WINP_SNIPPET_AUTO_HEADER: | |
| 79 | - case WINP_SNIPPET_AUTO_FOOTER: | |
| 80 | - $text = __( 'Everywhere', 'insert-php' ) . '[' . $items[ $snippet_location ] . ']'; | |
| 81 | - break; | |
| 82 | - | |
| 83 | - case WINP_SNIPPET_AUTO_BEFORE_POST: | |
| 84 | - case WINP_SNIPPET_AUTO_BEFORE_CONTENT: | |
| 85 | - case WINP_SNIPPET_AUTO_BEFORE_PARAGRAPH: | |
| 86 | - case WINP_SNIPPET_AUTO_AFTER_PARAGRAPH: | |
| 87 | - case WINP_SNIPPET_AUTO_AFTER_CONTENT: | |
| 88 | - case WINP_SNIPPET_AUTO_AFTER_POST: | |
| 89 | - $text = __( 'Posts, Pages, Custom post types', 'insert-php' ) . '[' . $items[ $snippet_location ] . ']'; | |
| 90 | - break; | |
| 91 | - | |
| 92 | - case WINP_SNIPPET_AUTO_BEFORE_EXCERPT: | |
| 93 | - case WINP_SNIPPET_AUTO_AFTER_EXCERPT: | |
| 94 | - case WINP_SNIPPET_AUTO_BETWEEN_POSTS: | |
| 95 | - case WINP_SNIPPET_AUTO_BEFORE_POSTS: | |
| 96 | - case WINP_SNIPPET_AUTO_AFTER_POSTS: | |
| 97 | - $text = __( 'Categories, Archives, Tags, Taxonomies', 'insert-php' ) . '[' . $items[ $snippet_location ] . ']'; | |
| 98 | - break; | |
| 99 | - | |
| 100 | - default: | |
| 101 | - $text = __( 'Everywhere', 'insert-php' ); | |
| 102 | - } | |
| 103 | - | |
| 104 | - echo __( 'Automatic insertion', 'insert-php' ) . ': ' . esc_html( $text ); | |
| 105 | - } else { | |
| 106 | - $snippet_type = WINP_Helper::get_snippet_type( $post->ID ); | |
| 107 | - $snippet_type = ( $snippet_type == WINP_SNIPPET_TYPE_UNIVERSAL ? '' : $snippet_type . '_' ); | |
| 108 | - | |
| 109 | - echo esc_html( apply_filters( 'wbcr/inp/viewtable/where_use', '[wbcr_' . $snippet_type . 'snippet id="' . $post->ID . '"]', $post->ID ) ); | |
| 110 | - } | |
| 111 | - } | |
| 112 | - | |
| 113 | - /** | |
| 114 | - * Column 'Actions' | |
| 115 | - * | |
| 116 | - * @param $post | |
| 117 | - */ | |
| 118 | - public function columnWinp_actions( $post ) { | |
| 119 | - $post_id = (int) $post->ID; | |
| 120 | - $is_activate = (int) WINP_Helper::getMetaOption( $post_id, 'snippet_activate', 0 ); | |
| 121 | - $icon = 'dashicons-controls-play'; | |
| 122 | - | |
| 123 | - if ( $is_activate ) { | |
| 124 | - $icon = 'dashicons-controls-pause'; | |
| 125 | - } | |
| 126 | - | |
| 127 | - 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>'; | |
| 128 | - } | |
| 129 | - | |
| 130 | - /* | |
| 131 | - * Activate/Deactivate snippet | |
| 132 | - */ | |
| 133 | - protected function runActions() { | |
| 134 | - if ( WINP_Plugin::app()->request->get( 'post_type', '', true ) == WINP_SNIPPETS_POST_TYPE ) { | |
| 135 | - $post = WINP_Plugin::app()->request->get( 'post', 0 ); | |
| 136 | - $action = WINP_Plugin::app()->request->get( 'action', '', 'sanitize_key' ); | |
| 137 | - | |
| 138 | - if ( ! empty( $action ) && ! empty( $post ) && 'wbcr_inp_activate_snippet' == $action ) { | |
| 139 | - $post_id = (int) $post; | |
| 140 | - $wpnonce = WINP_Plugin::app()->request->get( '_wpnonce', '' ); | |
| 141 | - | |
| 142 | - if ( ( ! empty( $wpnonce ) && ! wp_verify_nonce( $wpnonce, 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) ) || ! WINP_Plugin::app()->currentUserCan() ) { | |
| 143 | - wp_die( 'Permission error. You can not edit this page.' ); | |
| 144 | - } | |
| 145 | - | |
| 146 | - $is_activate = (int) WINP_Helper::getMetaOption( $post_id, 'snippet_activate', 0 ); | |
| 147 | - $snippet_scope = WINP_Helper::getMetaOption( $post_id, 'snippet_scope' ); | |
| 148 | - $snippet_type = WINP_Helper::get_snippet_type( $post_id ); | |
| 149 | - | |
| 150 | - /** | |
| 151 | - * Prevent activation of the snippet if it contains an error. This will not allow the user to break his site. | |
| 152 | - * | |
| 153 | - * @since 2.0.5 | |
| 154 | - */ | |
| 155 | - if ( ( 'evrywhere' == $snippet_scope || 'auto' == $snippet_scope ) && $snippet_type != WINP_SNIPPET_TYPE_TEXT && $snippet_type != WINP_SNIPPET_TYPE_CSS && $snippet_type != WINP_SNIPPET_TYPE_JS && ! $is_activate ) { | |
| 156 | - if ( WINP_Plugin::app()->getExecuteObject()->getSnippetError( $post_id ) ) { | |
| 157 | - wp_safe_redirect( add_query_arg( [ | |
| 158 | - 'action' => 'edit', | |
| 159 | - 'post' => $post_id, | |
| 160 | - 'wbcr_inp_save_snippet_result' => 'code-error', | |
| 161 | - ], admin_url( 'post.php' ) ) ); | |
| 162 | - exit; | |
| 163 | - } | |
| 164 | - } | |
| 165 | - | |
| 166 | - $status = ! $is_activate; | |
| 167 | - | |
| 168 | - update_post_meta( $post_id, $this->plugin->getPrefix() . 'snippet_activate', $status ); | |
| 169 | - | |
| 170 | - $redirect_url = add_query_arg( [ | |
| 171 | - 'post_type' => WINP_SNIPPETS_POST_TYPE, | |
| 172 | - 'wbcr_inp_snippet_updated' => 1, | |
| 173 | - ], admin_url( 'edit.php' ) ); | |
| 174 | - | |
| 175 | - wp_safe_redirect( $redirect_url ); | |
| 176 | - exit; | |
| 177 | - } | |
| 178 | - } | |
| 179 | - } | |
| 180 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Snippets View Table | |
| 4 | + * | |
| 5 | + * @package Woody_Code_Snippets | |
| 6 | + */ | |
| 7 | + | |
| 8 | +// Exit if accessed directly | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit; | |
| 11 | +} | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Class WINP_SnippetsViewTable | |
| 15 | + * | |
| 16 | + * Handles the custom columns and display for snippets list table. | |
| 17 | + */ | |
| 18 | +class WINP_SnippetsViewTable { | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Custom columns. | |
| 22 | + * | |
| 23 | + * @var array<string, string> | |
| 24 | + */ | |
| 25 | + private $columns = []; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Constructor. | |
| 29 | + */ | |
| 30 | + public function __construct() { | |
| 31 | + $this->setup_hooks(); | |
| 32 | + } | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Setup WordPress hooks. | |
| 36 | + * | |
| 37 | + * @return void | |
| 38 | + */ | |
| 39 | + private function setup_hooks() { | |
| 40 | + // Define columns. | |
| 41 | + $this->columns = [ | |
| 42 | + 'winp_actions' => __( 'Status', 'insert-php' ), | |
| 43 | + 'title' => __( 'Snippet title', 'insert-php' ), | |
| 44 | + 'winp_description' => __( 'Description', 'insert-php' ), | |
| 45 | + 'winp_where_use' => __( 'Location', 'insert-php' ), | |
| 46 | + 'winp_taxonomy' => __( 'Tags', 'insert-php' ), | |
| 47 | + 'winp_priority' => __( 'Priority', 'insert-php' ), | |
| 48 | + 'winp_snippet_type' => '', | |
| 49 | + ]; | |
| 50 | + | |
| 51 | + // Columns. | |
| 52 | + add_filter( 'manage_edit-' . WINP_SNIPPETS_POST_TYPE . '_columns', [ $this, 'setup_columns' ] ); | |
| 53 | + add_action( 'manage_' . WINP_SNIPPETS_POST_TYPE . '_posts_custom_column', [ $this, 'render_column' ], 10, 2 ); | |
| 54 | + add_filter( 'manage_edit-' . WINP_SNIPPETS_POST_TYPE . '_sortable_columns', [ $this, 'sortable_columns' ] ); | |
| 55 | + | |
| 56 | + // Scripts and styles. | |
| 57 | + add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); | |
| 58 | + | |
| 59 | + // Row actions. | |
| 60 | + add_filter( 'post_row_actions', [ $this, 'modify_row_actions' ], 10, 2 ); | |
| 61 | + add_filter( 'bulk_actions-edit-' . WINP_SNIPPETS_POST_TYPE, [ $this, 'modify_bulk_actions' ] ); | |
| 62 | + | |
| 63 | + // AJAX actions. | |
| 64 | + add_action( 'wp_ajax_change_priority', [ $this, 'change_priority' ] ); | |
| 65 | + add_action( 'wp_ajax_change_snippet_status', [ $this, 'change_snippet_status' ] ); | |
| 66 | + | |
| 67 | + // Run actions on admin_init to ensure proper loading order. | |
| 68 | + add_action( 'admin_init', [ $this, 'run_actions' ] ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Setup custom columns. | |
| 73 | + * | |
| 74 | + * @param array<string, string> $columns Existing columns. | |
| 75 | + * @return array<string, string> Modified columns. | |
| 76 | + */ | |
| 77 | + public function setup_columns( $columns ) { | |
| 78 | + unset( $columns ); // Unused, we define our own columns. | |
| 79 | + $new_columns = []; | |
| 80 | + $new_columns['cb'] = '<input type="checkbox" />'; | |
| 81 | + | |
| 82 | + foreach ( $this->columns as $id => $title ) { | |
| 83 | + $new_columns[ $id ] = $title; | |
| 84 | + } | |
| 85 | + | |
| 86 | + return $new_columns; | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Render custom column content. | |
| 91 | + * | |
| 92 | + * @param string $column Column name. | |
| 93 | + * @param int $post_id Post ID. | |
| 94 | + * @return void | |
| 95 | + */ | |
| 96 | + public function render_column( $column, $post_id ) { | |
| 97 | + $post = get_post( $post_id ); | |
| 98 | + $method_name = 'column_' . $column; | |
| 99 | + | |
| 100 | + if ( method_exists( $this, $method_name ) && is_callable( [ $this, $method_name ] ) ) { | |
| 101 | + $this->{$method_name}( $post ); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Enqueue scripts and styles. | |
| 107 | + * | |
| 108 | + * @param string $hook Current admin page hook. | |
| 109 | + * @return void | |
| 110 | + */ | |
| 111 | + public function enqueue_assets( $hook ) { | |
| 112 | + if ( 'edit.php' !== $hook ) { | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + | |
| 116 | + $screen = get_current_screen(); | |
| 117 | + if ( ! $screen || WINP_SNIPPETS_POST_TYPE !== $screen->post_type ) { | |
| 118 | + return; | |
| 119 | + } | |
| 120 | + | |
| 121 | + wp_enqueue_style( 'winp-list-table', WINP_PLUGIN_URL . '/admin/assets/css/list-table.css', [], WINP_PLUGIN_VERSION ); | |
| 122 | + wp_enqueue_script( 'winp-snippet-list', WINP_PLUGIN_URL . '/admin/assets/js/snippet-list.js', [ 'jquery' ], WINP_PLUGIN_VERSION, true ); | |
| 123 | + wp_localize_script( 'winp-snippet-list', 'winp_ajax', [ 'nonce' => wp_create_nonce( 'winp_ajax' ) ] ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * Modify row actions. | |
| 128 | + * | |
| 129 | + * @param array<string, string> $actions Existing actions. | |
| 130 | + * @param WP_Post $post Post object. | |
| 131 | + * @return array<string, string> Modified actions. | |
| 132 | + */ | |
| 133 | + public function modify_row_actions( $actions, $post ) { | |
| 134 | + if ( WINP_SNIPPETS_POST_TYPE !== $post->post_type ) { | |
| 135 | + return $actions; | |
| 136 | + } | |
| 137 | + | |
| 138 | + // Remove quick edit for non-public types. | |
| 139 | + unset( $actions['inline hide-if-no-js'] ); | |
| 140 | + | |
| 141 | + return $actions; | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Modify bulk actions. | |
| 146 | + * | |
| 147 | + * @param array<string, string> $actions Existing actions. | |
| 148 | + * @return array<string, string> Modified actions. | |
| 149 | + */ | |
| 150 | + public function modify_bulk_actions( $actions ) { | |
| 151 | + // Remove bulk edit. | |
| 152 | + unset( $actions['edit'] ); | |
| 153 | + | |
| 154 | + return $actions; | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * Column 'Type' | |
| 159 | + * | |
| 160 | + * @param WP_Post $post Post object. | |
| 161 | + * @return void | |
| 162 | + */ | |
| 163 | + public function column_winp_snippet_type( $post ) { | |
| 164 | + $type = WINP_Helper::getMetaOption( $post->ID, 'snippet_type', WINP_SNIPPET_TYPE_PHP ); | |
| 165 | + $class = 'wbcr-inp-type-' . esc_attr( $type ); | |
| 166 | + $type = $type == 'universal' ? 'uni' : $type; | |
| 167 | + $type = $type == 'advert' ? 'ad' : $type; | |
| 168 | + | |
| 169 | + echo '<div class="wbcr-inp-snippet-type-label ' . esc_attr( $class ) . '">' . esc_html( $type ) . '</div>'; | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * Column 'Description' | |
| 174 | + * | |
| 175 | + * @param WP_Post $post Post object. | |
| 176 | + * @return void | |
| 177 | + */ | |
| 178 | + public function column_winp_description( $post ) { | |
| 179 | + echo esc_html( WINP_Helper::getMetaOption( $post->ID, 'snippet_description' ) ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Column 'Where_use' | |
| 184 | + * | |
| 185 | + * @param WP_Post $post Post object. | |
| 186 | + * @return void | |
| 187 | + */ | |
| 188 | + public function column_winp_where_use( $post ) { | |
| 189 | + $value = WINP_Helper::get_where_use_text( $post ); | |
| 190 | + | |
| 191 | + $is_shortcode = ( strpos( $value, '[' ) === 0 && strrpos( $value, ']' ) === strlen( $value ) - 1 ); | |
| 192 | + | |
| 193 | + if ( $is_shortcode ) { | |
| 194 | + echo "<div style='position: relative; display: inline-flex; align-items: center; width: 100%;'>"; | |
| 195 | + echo "<span class='dashicons dashicons-clipboard' style='position: absolute; left: 8px; pointer-events: none; color: #2271b1;'></span>"; | |
| 196 | + echo "<input type='text' name='wbcr_inp_shortcode_input' class='wbcr_inp_shortcode_input' value='" . esc_attr( $value ) . "' readonly='readonly' style='cursor: pointer; padding-left: 35px; width: 100%;'>"; | |
| 197 | + echo '</div>'; | |
| 198 | + } else { | |
| 199 | + echo esc_html( $value ); | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + /** | |
| 204 | + * Column 'Taxonomy' | |
| 205 | + * | |
| 206 | + * @param WP_Post $post Post object. | |
| 207 | + * @return void | |
| 208 | + */ | |
| 209 | + public function column_winp_taxonomy( $post ) { | |
| 210 | + $post_cat = get_the_terms( $post->ID, WINP_SNIPPETS_TAXONOMY ); | |
| 211 | + $result = []; | |
| 212 | + if ( is_array( $post_cat ) ) { | |
| 213 | + foreach ( $post_cat as $item ) { | |
| 214 | + $href = admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . "&winp_filter_tag={$item->slug}" ); | |
| 215 | + $result[] = "<a href='{$href}' class='winp-taxonomy-href'>{$item->name}</a>"; | |
| 216 | + } | |
| 217 | + } | |
| 218 | + echo implode( ', ', $result ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * Column 'Priority' | |
| 223 | + * | |
| 224 | + * @param WP_Post $post Post object. | |
| 225 | + * @return void | |
| 226 | + */ | |
| 227 | + public function column_winp_priority( $post ) { | |
| 228 | + $snippet_priority = WINP_Helper::getMetaOption( $post->ID, 'snippet_priority' ); | |
| 229 | + echo "<input type='number' name='wbcr_inp_input_priority' class='wbcr_inp_input_priority' | |
| 230 | + data-snippet-id='{$post->ID}' value='{$snippet_priority}'>"; | |
| 231 | + } | |
| 232 | + | |
| 233 | + /** | |
| 234 | + * Column 'Actions' | |
| 235 | + * | |
| 236 | + * @param WP_Post $post Post object. | |
| 237 | + * @return void | |
| 238 | + */ | |
| 239 | + public function column_winp_actions( $post ) { | |
| 240 | + $post_id = (int) $post->ID; | |
| 241 | + $is_activate = (int) WINP_Helper::getMetaOption( $post_id, 'snippet_activate', 0 ); | |
| 242 | + $css_class = 'winp-inactive'; | |
| 243 | + | |
| 244 | + if ( $is_activate ) { | |
| 245 | + $css_class = ''; | |
| 246 | + } | |
| 247 | + | |
| 248 | + $url = wp_nonce_url( | |
| 249 | + admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&post=' . $post_id . '&action=wbcr_inp_activate_snippet' ), | |
| 250 | + 'wbcr_inp_snippert_' . $post_id . '_action_nonce' | |
| 251 | + ); | |
| 252 | + | |
| 253 | + echo '<a class="winp-snippet-active-switch ' . esc_attr( $css_class ) . '" id="winp-snippet-status-switch" data-snippet-id="' . esc_attr( (string) $post_id ) . '" href="' . esc_url( $url ) . '"> </a>'; | |
| 254 | + } | |
| 255 | + | |
| 256 | + /** | |
| 257 | + * Activate/Deactivate snippet | |
| 258 | + * | |
| 259 | + * @return void | |
| 260 | + */ | |
| 261 | + public function run_actions() { | |
| 262 | + if ( WINP_HTTP::get( 'post_type', '', true ) == WINP_SNIPPETS_POST_TYPE ) { | |
| 263 | + $post = WINP_HTTP::get( 'post', 0 ); | |
| 264 | + $action = WINP_HTTP::get( 'action', '', 'sanitize_key' ); | |
| 265 | + | |
| 266 | + if ( ! empty( $action ) && ! empty( $post ) && 'wbcr_inp_activate_snippet' == $action ) { | |
| 267 | + $post_id = (int) $post; | |
| 268 | + $wpnonce = WINP_HTTP::get( '_wpnonce', '' ); | |
| 269 | + | |
| 270 | + if ( ! wp_verify_nonce( $wpnonce, 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) || ! WINP_Plugin::app()->current_user_car() ) { | |
| 271 | + wp_die( 'Permission error. You can not edit this page.' ); | |
| 272 | + } | |
| 273 | + | |
| 274 | + $is_activate = (int) WINP_Helper::getMetaOption( $post_id, 'snippet_activate', 0 ); | |
| 275 | + $snippet_scope = WINP_Helper::getMetaOption( $post_id, 'snippet_scope' ); | |
| 276 | + $snippet_type = WINP_Helper::get_snippet_type( $post_id ); | |
| 277 | + | |
| 278 | + /** | |
| 279 | + * Prevent activation of the snippet if it contains an error. This will not allow the user to break his site. | |
| 280 | + * | |
| 281 | + * @since 2.0.5 | |
| 282 | + */ | |
| 283 | + 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 ) { | |
| 284 | + if ( WINP_Plugin::app()->get_execute_object()->getSnippetError( $post_id ) ) { | |
| 285 | + wp_safe_redirect( | |
| 286 | + add_query_arg( | |
| 287 | + [ | |
| 288 | + 'action' => 'edit', | |
| 289 | + 'post' => $post_id, | |
| 290 | + 'wbcr_inp_save_snippet_result' => 'code-error', | |
| 291 | + ], | |
| 292 | + admin_url( 'post.php' ) | |
| 293 | + ) | |
| 294 | + ); | |
| 295 | + exit; | |
| 296 | + } | |
| 297 | + } | |
| 298 | + | |
| 299 | + $status = ! $is_activate; | |
| 300 | + | |
| 301 | + update_post_meta( $post_id, 'wbcr_inp_snippet_activate', $status ); | |
| 302 | + | |
| 303 | + $redirect_url = add_query_arg( | |
| 304 | + [ | |
| 305 | + 'post_type' => WINP_SNIPPETS_POST_TYPE, | |
| 306 | + 'wbcr_inp_snippet_updated' => 1, | |
| 307 | + ], | |
| 308 | + admin_url( 'edit.php' ) | |
| 309 | + ); | |
| 310 | + | |
| 311 | + wp_safe_redirect( $redirect_url ); | |
| 312 | + exit; | |
| 313 | + } | |
| 314 | + } | |
| 315 | + } | |
| 316 | + | |
| 317 | + /** | |
| 318 | + * Make columns sortable. | |
| 319 | + * | |
| 320 | + * @param array<string, string> $sortable_columns Existing sortable columns. | |
| 321 | + * @return array<string, string> Modified sortable columns. | |
| 322 | + */ | |
| 323 | + public function sortable_columns( $sortable_columns ) { | |
| 324 | + $sortable_columns['winp_priority'] = 'winp_priority'; | |
| 325 | + | |
| 326 | + return $sortable_columns; | |
| 327 | + } | |
| 328 | + | |
| 329 | + /** | |
| 330 | + * AJAX action for change priority. | |
| 331 | + * | |
| 332 | + * @return void | |
| 333 | + */ | |
| 334 | + public function change_priority() { | |
| 335 | + check_ajax_referer( 'winp_ajax' ); | |
| 336 | + | |
| 337 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 338 | + wp_send_json( [ 'error_message' => __( "You don't have permission to edit this.", 'insert-php' ) ] ); | |
| 339 | + } | |
| 340 | + | |
| 341 | + if ( isset( $_POST['snippet_id'] ) && isset( $_POST['priority'] ) ) { | |
| 342 | + if ( is_numeric( $_POST['priority'] ) ) { | |
| 343 | + WINP_Helper::updateMetaOption( $_POST['snippet_id'], 'snippet_priority', $_POST['priority'] ); | |
| 344 | + | |
| 345 | + wp_send_json( | |
| 346 | + [ | |
| 347 | + 'message' => __( 'Priority successfully changed', 'insert-php' ), | |
| 348 | + ] | |
| 349 | + ); | |
| 350 | + } else { | |
| 351 | + wp_send_json( | |
| 352 | + [ | |
| 353 | + 'error_message' => __( 'Priority was not changed. It must be a number.', 'insert-php' ), | |
| 354 | + ] | |
| 355 | + ); | |
| 356 | + } | |
| 357 | + } else { | |
| 358 | + wp_send_json( | |
| 359 | + [ | |
| 360 | + 'error_message' => __( 'Priority is not changed!', 'insert-php' ), | |
| 361 | + ] | |
| 362 | + ); | |
| 363 | + } | |
| 364 | + } | |
| 365 | + | |
| 366 | + /** | |
| 367 | + * AJAX action for change snippet status. | |
| 368 | + * | |
| 369 | + * @return void | |
| 370 | + */ | |
| 371 | + public function change_snippet_status() { | |
| 372 | + check_ajax_referer( 'winp_ajax' ); | |
| 373 | + | |
| 374 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 375 | + wp_send_json( [ 'error_message' => __( "You don't have permission to edit this.", 'insert-php' ) ] ); | |
| 376 | + } | |
| 377 | + | |
| 378 | + if ( isset( $_POST['snippet_id'] ) ) { | |
| 379 | + $snippet_id = $_POST['snippet_id']; | |
| 380 | + $is_activate = (int) WINP_Helper::getMetaOption( $snippet_id, 'snippet_activate', 0 ); | |
| 381 | + $snippet_scope = WINP_Helper::getMetaOption( $snippet_id, 'snippet_scope' ); | |
| 382 | + $snippet_type = WINP_Helper::get_snippet_type( $snippet_id ); | |
| 383 | + | |
| 384 | + /** | |
| 385 | + * Prevent activation of the snippet if it contains an error. This will not allow the user to break his site. | |
| 386 | + * | |
| 387 | + * @since 2.0.5 | |
| 388 | + */ | |
| 389 | + 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 ) { | |
| 390 | + if ( WINP_Plugin::app()->get_execute_object()->getSnippetError( $snippet_id ) ) { | |
| 391 | + wp_send_json( | |
| 392 | + [ | |
| 393 | + 'alert' => true, | |
| 394 | + 'error_message' => __( 'This snippet was not activated due to code errors. Please fix the errors and try again.', 'insert-php' ), | |
| 395 | + ] | |
| 396 | + ); | |
| 397 | + } | |
| 398 | + } | |
| 399 | + | |
| 400 | + $status = ! $is_activate; | |
| 401 | + | |
| 402 | + $ok = update_post_meta( $snippet_id, 'wbcr_inp_snippet_activate', $status ); | |
| 403 | + | |
| 404 | + if ( $ok ) { | |
| 405 | + wp_send_json( | |
| 406 | + [ | |
| 407 | + 'message' => __( 'Snippet status changed', 'insert-php' ), | |
| 408 | + ] | |
| 409 | + ); | |
| 410 | + } else { | |
| 411 | + wp_send_json( | |
| 412 | + [ | |
| 413 | + 'error_message' => __( 'Snippet status could not be changed. Please try again or check your permissions.', 'insert-php' ), | |
| 414 | + ] | |
| 415 | + ); | |
| 416 | + | |
| 417 | + } | |
| 418 | + } else { | |
| 419 | + wp_send_json( | |
| 420 | + [ | |
| 421 | + 'error_message' => __( 'Could not update snippet status. Please refresh and try again.', 'insert-php' ), | |
| 422 | + ] | |
| 423 | + ); | |
| 424 | + } | |
| 425 | + } | |
| 426 | +} | |