page_title = __( 'Code Snippets', 'insert-headers-and-footers' ); parent::__construct(); } /** * Page-specific hooks, init the custom WP_List_Table. * * @return void */ public function page_hooks() { $this->process_message(); add_action( 'current_screen', array( $this, 'init_table' ) ); add_action( 'admin_init', array( $this, 'maybe_capture_filter' ) ); add_action( 'load-toplevel_page_wpcode', array( $this, 'maybe_process_bulk_action' ) ); add_action( 'wpcode_admin_notices', array( $this, 'maybe_show_deactivated_notice' ) ); // Register Screen options. add_action( 'load-toplevel_page_wpcode', array( $this, 'add_per_page_option' ) ); // Hide some columns by default. add_filter( 'default_hidden_columns', array( $this, 'hide_columns' ), 10, 2 ); add_filter( 'screen_settings', array( $this, 'add_custom_screen_option' ), 10, 2 ); // Hide the duplicated parameter from the URL. add_filter( 'removable_query_args', array( $this, 'remove_query_arg_from_url' ) ); } /** * If the referer is set, remove and redirect. * * @return void */ public function maybe_capture_filter() { if ( ! empty( $_REQUEST['_wp_http_referer'] ) && isset( $_SERVER['REQUEST_URI'] ) && isset( $_REQUEST['filter_action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended wp_safe_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce', ), wp_unslash( $_SERVER['REQUEST_URI'] ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized ) ); exit; } if ( ! empty( $_REQUEST['_wp_http_referer'] ) && isset( $_SERVER['REQUEST_URI'] ) && isset( $_REQUEST['filter_clear'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended wp_safe_redirect( add_query_arg( 'page', 'wpcode', $this->admin_url( 'admin.php' ) ) ); exit; } } /** * Listener for bulk actions. * * @return void */ public function maybe_process_bulk_action() { // phpcs:disable WordPress.Security.NonceVerification.Recommended $ids = isset( $_GET['snippet_id'] ) ? array_map( 'absint', (array) $_GET['snippet_id'] ) : array(); $action = isset( $_REQUEST['action'] ) ? sanitize_key( $_REQUEST['action'] ) : false; // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( empty( $ids ) || empty( $action ) ) { return; } if ( empty( $_GET['_wpnonce'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'bulk-wpcode-snippets' ) && ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'wpcode_' . $action . '_nonce' ) ) { return; } $update_status_actions = array( 'trash', 'untrash' ); if ( in_array( $action, $update_status_actions, true ) ) { $newstatus = 'trash' === $action ? 'trash' : 'draft'; foreach ( $ids as $id ) { wp_update_post( array( 'ID' => $id, 'post_status' => $newstatus, ) ); } // Clear errors when a snippet is trashed. wpcode()->error->clear_snippets_errors(); } if ( 'delete' === $action ) { foreach ( $ids as $id ) { wp_delete_post( $id ); } // Clear errors when a snippet is deleted. wpcode()->error->clear_snippets_errors(); } $failed = 0; if ( 'enable' === $action ) { foreach ( $ids as $key => $id ) { $snippet = wpcode_get_snippet( $id ); $snippet->activate(); if ( ! $snippet->active ) { // If failed to activate don't count it. unset( $ids[ $key ] ); $failed ++; } } } if ( 'disable' === $action ) { foreach ( $ids as $id ) { $snippet = wpcode_get_snippet( $id ); $snippet->deactivate(); } } $message = array( rtrim( $action, 'e' ) . 'ed' => count( $ids ), ); if ( $failed ) { $message['error'] = $failed; } if ( 'duplicate' === $action ) { foreach ( $ids as $id ) { // Load all the snippet data in the object. $snippet = wpcode_get_snippet( $id ); $snippet->duplicate(); } } wpcode()->cache->cache_all_loaded_snippets(); // Clear used library snippets. delete_transient( 'wpcode_used_library_snippets' ); wp_safe_redirect( add_query_arg( $message, remove_query_arg( array( 'action', 'action2', '_wpnonce', 'snippet_id', 'paged', '_wp_http_referer', ) ) ) ); exit; } /** * Init the custom table for the snippets list. * * @return void */ public function init_table() { require_once WPCODE_PLUGIN_PATH . 'includes/admin/pages/class-wpcode-code-snippets-table.php'; $this->snippets_table = new WPCode_Code_Snippets_Table(); } /** * Output the custom table and page content. * * @return void */ public function output_content() { $this->snippets_table->prepare_items(); ?>
snippets_table->search_box( esc_html__( 'Search Snippets', 'insert-headers-and-footers' ), 'wpcode_snippet_search' ); $this->snippets_table->views(); $this->snippets_table->display(); ?>
admin_url( 'admin.php?page=wpcode-snippet-manager' ); ?>

0 ) { $notice = sprintf( /* translators: %d - Activated snippets count. */ _n( '%d snippet was successfully activated.', '%d snippets were successfully activated.', $count, 'insert-headers-and-footers' ), $count ); } if ( isset( $_GET['error'] ) ) { $error_count = absint( $_GET['error'] ); $notice .= ' '; $notice .= sprintf( /* translators: %d - Failed to activate snippets count. */ _n( '%d snippet was not activated due to an error.', '%d snippets were not activated due to errors.', $error_count, 'insert-headers-and-footers' ), $error_count ); } } if ( ! empty( $_GET['disabled'] ) ) { $count = absint( $_GET['disabled'] ); $notice = sprintf( /* translators: %d - Deactivated snippets count. */ _n( '%d snippet was successfully deactivated.', '%d snippets were successfully deactivated.', $count, 'insert-headers-and-footers' ), $count ); } // phpcs:enable WordPress.Security.NonceVerification if ( isset( $error_count ) && isset( $notice ) ) { $this->set_error_message( $notice ); } elseif ( isset( $notice ) ) { $this->set_success_message( $notice ); } } /** * On the deactivated snippets view, show a notice explaining that this view shows the snippets that have been * automatically disabled due to throwing an error and highlight the error logging option, if disabled. * * @return void */ public function maybe_show_deactivated_notice() { if ( ! isset( $_GET['view'] ) || 'has_error' !== $_GET['view'] ) { // phpcs:ignore WordPress.Security.NonceVerification return; } // Let's see if error logging is enabled. $logging_enabled = wpcode()->settings->get_option( 'error_logging' ); $button_text = esc_html__( 'Enable Error Logging', 'insert-headers-and-footers' ); $button_url = add_query_arg( array( 'page' => 'wpcode-settings', 'view' => 'errors', ), $this->admin_url( 'admin.php' ) ); ?>

esc_html__( 'Number of snippets per page:', 'insert-headers-and-footers' ), 'option' => 'wpcode_snippets_per_page', 'default' => 20, ) ); } /** * Hide the last updated column by default. * * @param array $hidden The hidden columns. * @param WP_Screen $screen The current screen. * * @return mixed */ public function hide_columns( $hidden, $screen ) { $hidden[] = 'updated'; $hidden[] = 'id'; $hidden[] = 'shortcode'; $hidden[] = 'code_type'; return $hidden; } /** * * @param string $screen_settings Screen settings. * @param WP_Screen $screen WP_Screen object. * * @return string */ public function add_custom_screen_option( $screen_settings, $screen ) { $order_by = get_user_option( 'wpcode_snippets_order_by' ); $order = get_user_option( 'wpcode_snippets_order' ); if ( empty( $order_by ) ) { $order_by = 'ID'; } if ( empty( $order ) ) { $order = 'desc'; } // Pick which column to order by, title, date or last updated using a select. $screen_settings .= '
' . esc_html__( 'Order Snippets By', 'insert-headers-and-footers' ) . '
'; $screen_settings .= '
'; $screen_settings .= '' . esc_html__( 'Order snippets by', 'insert-headers-and-footers' ) . ''; // Use dropdown to choose the column to order by. $screen_settings .= ''; // Display a dropdown to choose the order. $screen_settings .= ''; $screen_settings .= '
'; return $screen_settings; } /** * Remove the duplicated parameter from the URL. * * @param array $args The arguments that should be removed from the URL. * * @return array */ public function remove_query_arg_from_url( $args ) { $args[] = 'duplicated'; return $args; } }