PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / trunk
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts vtrunk
2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 All 28 releases
← All changes | admin/includes/class.snippets.viewtable.php +260 -102 2.4.4trunk View file →
@@ -1,5 +1,10 @@
1 1 <?php
2 +/**
3 + * Snippets View Table
4 + *
5 + * @package Woody_Code_Snippets
6 + */
2 7
3 8 // Exit if accessed directly
4 9 if ( ! defined( 'ABSPATH' ) ) {
5 10 exit;
@@ -4,47 +9,159 @@
4 9 if ( ! defined( 'ABSPATH' ) ) {
5 10 exit;
6 11 }
7 12
8 -class WINP_SnippetsViewTable extends Wbcr_FactoryViewtables410_Viewtable {
13 +/**
14 + * Class WINP_SnippetsViewTable
15 + *
16 + * Handles the custom columns and display for snippets list table.
17 + */
18 +class WINP_SnippetsViewTable {
9 19
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', '' );
20 + /**
21 + * Custom columns.
22 + *
23 + * @var array<string, string>
24 + */
25 + private $columns = [];
23 26
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();
27 + /**
28 + * Constructor.
29 + */
30 + public function __construct() {
31 + $this->setup_hooks();
32 + }
31 33
32 - add_filter( 'manage_edit-' . WINP_SNIPPETS_POST_TYPE . '_sortable_columns', array(
33 - $this,
34 - 'sortable_columns'
35 - ) );
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.
36 64 add_action( 'wp_ajax_change_priority', [ $this, 'change_priority' ] );
37 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' ] );
38 69 }
39 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" />';
40 81
82 + foreach ( $this->columns as $id => $title ) {
83 + $new_columns[ $id ] = $title;
84 + }
85 +
86 + return $new_columns;
87 + }
88 +
41 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 + /**
42 158 * Column 'Type'
43 159 *
44 - * @param $post
160 + * @param WP_Post $post Post object.
161 + * @return void
45 162 */
46 - public function columnWinp_snippet_type( $post ) {
163 + public function column_winp_snippet_type( $post ) {
47 164 $type = WINP_Helper::getMetaOption( $post->ID, 'snippet_type', WINP_SNIPPET_TYPE_PHP );
48 165 $class = 'wbcr-inp-type-' . esc_attr( $type );
49 166 $type = $type == 'universal' ? 'uni' : $type;
50 167 $type = $type == 'advert' ? 'ad' : $type;
@@ -51,9 +168,15 @@
51 168
52 169 echo '<div class="wbcr-inp-snippet-type-label ' . esc_attr( $class ) . '">' . esc_html( $type ) . '</div>';
53 170 }
54 171
55 - public function columnWinp_description( $post ) {
172 + /**
173 + * Column 'Description'
174 + *
175 + * @param WP_Post $post Post object.
176 + * @return void
177 + */
178 + public function column_winp_description( $post ) {
56 179 echo esc_html( WINP_Helper::getMetaOption( $post->ID, 'snippet_description' ) );
57 180 }
58 181
59 182 /**
@@ -58,35 +181,38 @@
58 181
59 182 /**
60 183 * Column 'Where_use'
61 184 *
62 - * @param $post
185 + * @param WP_Post $post Post object.
186 + * @return void
63 187 */
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)'";
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 );
69 200 }
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 201 }
74 202
75 203 /**
76 - * Column 'Priority'
204 + * Column 'Taxonomy'
77 205 *
78 - * @param $post
79 - *
80 - * @since 2.4.0
81 - *
206 + * @param WP_Post $post Post object.
207 + * @return void
82 208 */
83 - public function columnWinp_taxonomy( $post ) {
209 + public function column_winp_taxonomy( $post ) {
84 210 $post_cat = get_the_terms( $post->ID, WINP_SNIPPETS_TAXONOMY );
85 211 $result = [];
86 212 if ( is_array( $post_cat ) ) {
87 213 foreach ( $post_cat as $item ) {
88 - $href = admin_url( "edit.php?post_type=" . WINP_SNIPPETS_POST_TYPE . "&winp_filter_tag={$item->slug}" );
214 + $href = admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . "&winp_filter_tag={$item->slug}" );
89 215 $result[] = "<a href='{$href}' class='winp-taxonomy-href'>{$item->name}</a>";
90 216 }
91 217 }
92 218 echo implode( ', ', $result );
@@ -94,14 +220,12 @@
94 220
95 221 /**
96 222 * Column 'Priority'
97 223 *
98 - * @param $post
99 - *
100 - * @since 2.4.0
101 - *
224 + * @param WP_Post $post Post object.
225 + * @return void
102 226 */
103 - public function columnWinp_priority( $post ) {
227 + public function column_winp_priority( $post ) {
104 228 $snippet_priority = WINP_Helper::getMetaOption( $post->ID, 'snippet_priority' );
105 229 echo "<input type='number' name='wbcr_inp_input_priority' class='wbcr_inp_input_priority'
106 230 data-snippet-id='{$post->ID}' value='{$snippet_priority}'>";
107 231 }
@@ -108,11 +232,12 @@
108 232
109 233 /**
110 234 * Column 'Actions'
111 235 *
112 - * @param $post
236 + * @param WP_Post $post Post object.
237 + * @return void
113 238 */
114 - public function columnWinp_actions( $post ) {
239 + public function column_winp_actions( $post ) {
115 240 $post_id = (int) $post->ID;
116 241 $is_activate = (int) WINP_Helper::getMetaOption( $post_id, 'snippet_activate', 0 );
117 242 $css_class = 'winp-inactive';
118 243
@@ -119,25 +244,31 @@
119 244 if ( $is_activate ) {
120 245 $css_class = '';
121 246 }
122 247
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 . '&amp;post=' . $post_id . '&amp;action=wbcr_inp_activate_snippet' ), 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) . "'>&nbsp;</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 . '&amp;post=' . $post_id . '&amp;action=wbcr_inp_activate_snippet' ), 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) . '"><span class="dashicons ' . esc_attr( $icon ) . '"></span></a>';
248 + $url = wp_nonce_url(
249 + admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&amp;post=' . $post_id . '&amp;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 ) . '">&nbsp;</a>';
125 254 }
126 255
127 - /*
256 + /**
128 257 * Activate/Deactivate snippet
258 + *
259 + * @return void
129 260 */
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' );
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' );
134 265
135 266 if ( ! empty( $action ) && ! empty( $post ) && 'wbcr_inp_activate_snippet' == $action ) {
136 267 $post_id = (int) $post;
137 - $wpnonce = WINP_Plugin::app()->request->get( '_wpnonce', '' );
268 + $wpnonce = WINP_HTTP::get( '_wpnonce', '' );
138 269
139 - if ( ! wp_verify_nonce( $wpnonce, 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) || ! WINP_Plugin::app()->currentUserCan() ) {
270 + if ( ! wp_verify_nonce( $wpnonce, 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) || ! WINP_Plugin::app()->current_user_car() ) {
140 271 wp_die( 'Permission error. You can not edit this page.' );
141 272 }
142 273
143 274 $is_activate = (int) WINP_Helper::getMetaOption( $post_id, 'snippet_activate', 0 );
@@ -149,14 +280,19 @@
149 280 *
150 281 * @since 2.0.5
151 282 */
152 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 ) {
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' ) ) );
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 + );
159 295 exit;
160 296 }
161 297 }
162 298
@@ -161,14 +297,17 @@
161 297 }
162 298
163 299 $status = ! $is_activate;
164 300
165 - update_post_meta( $post_id, $this->plugin->getPrefix() . 'snippet_activate', $status );
301 + update_post_meta( $post_id, 'wbcr_inp_snippet_activate', $status );
166 302
167 - $redirect_url = add_query_arg( [
168 - 'post_type' => WINP_SNIPPETS_POST_TYPE,
169 - 'wbcr_inp_snippet_updated' => 1,
170 - ], admin_url( 'edit.php' ) );
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 + );
171 310
172 311 wp_safe_redirect( $redirect_url );
173 312 exit;
174 313 }
@@ -175,9 +314,12 @@
175 314 }
176 315 }
177 316
178 317 /**
179 - * @param $sortable_columns
318 + * Make columns sortable.
319 + *
320 + * @param array<string, string> $sortable_columns Existing sortable columns.
321 + * @return array<string, string> Modified sortable columns.
180 322 */
181 323 public function sortable_columns( $sortable_columns ) {
182 324 $sortable_columns['winp_priority'] = 'winp_priority';
183 325
@@ -184,15 +326,17 @@
184 326 return $sortable_columns;
185 327 }
186 328
187 329 /**
188 - * AJAX action for change priority
330 + * AJAX action for change priority.
331 + *
332 + * @return void
189 333 */
190 334 public function change_priority() {
191 335 check_ajax_referer( 'winp_ajax' );
192 336
193 337 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' ) ) );
338 + wp_send_json( [ 'error_message' => __( "You don't have permission to edit this.", 'insert-php' ) ] );
195 339 }
196 340
197 341 if ( isset( $_POST['snippet_id'] ) && isset( $_POST['priority'] ) ) {
198 342 if ( is_numeric( $_POST['priority'] ) ) {
@@ -197,32 +341,39 @@
197 341 if ( isset( $_POST['snippet_id'] ) && isset( $_POST['priority'] ) ) {
198 342 if ( is_numeric( $_POST['priority'] ) ) {
199 343 WINP_Helper::updateMetaOption( $_POST['snippet_id'], 'snippet_priority', $_POST['priority'] );
200 344
201 - wp_send_json( [
202 - 'message' => __( "Priority successfully changed", "insert-php" ),
203 - ] );
345 + wp_send_json(
346 + [
347 + 'message' => __( 'Priority successfully changed', 'insert-php' ),
348 + ]
349 + );
204 350 } else {
205 - wp_send_json( [
206 - 'error_message' => __( "Priority is not changed! It's must be a number", 'insert-php' ),
207 - ] );
208 - }
209 -
351 + wp_send_json(
352 + [
353 + 'error_message' => __( 'Priority was not changed. It must be a number.', 'insert-php' ),
354 + ]
355 + );
356 + }
210 357 } else {
211 - wp_send_json( [
358 + wp_send_json(
359 + [
212 360 'error_message' => __( 'Priority is not changed!', 'insert-php' ),
213 - ] );
361 + ]
362 + );
214 363 }
215 364 }
216 365
217 366 /**
218 - * AJAX action for change priority
367 + * AJAX action for change snippet status.
368 + *
369 + * @return void
219 370 */
220 371 public function change_snippet_status() {
221 372 check_ajax_referer( 'winp_ajax' );
222 373
223 374 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' ) ) );
375 + wp_send_json( [ 'error_message' => __( "You don't have permission to edit this.", 'insert-php' ) ] );
225 376 }
226 377
227 378 if ( isset( $_POST['snippet_id'] ) ) {
228 379 $snippet_id = $_POST['snippet_id'];
@@ -235,34 +386,41 @@
235 386 *
236 387 * @since 2.0.5
237 388 */
238 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 ) {
239 - if ( WINP_Plugin::app()->getExecuteObject()->getSnippetError( $snippet_id ) ) {
240 - wp_send_json( [
390 + if ( WINP_Plugin::app()->get_execute_object()->getSnippetError( $snippet_id ) ) {
391 + wp_send_json(
392 + [
241 393 'alert' => true,
242 - 'error_message' => __( "The snippet is not activated because errors were detected in the snippet code!", 'insert-php' ),
243 - ] );
394 + 'error_message' => __( 'This snippet was not activated due to code errors. Please fix the errors and try again.', 'insert-php' ),
395 + ]
396 + );
244 397 }
245 398 }
246 399
247 400 $status = ! $is_activate;
248 401
249 - $ok = update_post_meta( $snippet_id, $this->plugin->getPrefix() . 'snippet_activate', $status );
402 + $ok = update_post_meta( $snippet_id, 'wbcr_inp_snippet_activate', $status );
250 403
251 404 if ( $ok ) {
252 - wp_send_json( [
253 - 'message' => __( "Snippet status changed", "insert-php" ),
254 - ] );
405 + wp_send_json(
406 + [
407 + 'message' => __( 'Snippet status changed', 'insert-php' ),
408 + ]
409 + );
255 410 } else {
256 - wp_send_json( [
257 - 'error_message' => __( 'Snippet status not changed.', 'insert-php' ),
258 - ] );
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 + );
259 416
260 - }
261 -
417 + }
262 418 } else {
263 - wp_send_json( [
264 - 'error_message' => __( 'Snippet status not changed. No snippet ID', 'insert-php' ),
265 - ] );
419 + wp_send_json(
420 + [
421 + 'error_message' => __( 'Could not update snippet status. Please refresh and try again.', 'insert-php' ),
422 + ]
423 + );
266 424 }
267 425 }
268 426 }