$columns['cb'], 'enabled' => '' . __( 'Status', 'add-to-all' ) . '', ); // Loop through the existing columns and evaluate the column. foreach ( $columns as $key => $title ) { if ( 'cb' !== $key ) { $new_columns[ $key ] = $title; } if ( 'title' === $key ) { $new_columns['type'] = __( 'Type', 'add-to-all' ); $new_columns['shortcode'] = __( 'Shortcode', 'add-to-all' ); if ( ata_get_option( 'enable_external_css_js' ) ) { $new_columns['external_file'] = __( 'External File', 'add-to-all' ); } } } return $new_columns; } /** * Make custom column sortable. * * @param array $columns The existing sortable columns. * @return array The modified sortable columns. */ public function set_sortable_columns( $columns ) { $columns['type'] = 'type'; return $columns; } /** * Add custom column content. * * @param string $column_name Column name. * @param int $post_id Post ID. */ public function manage_posts_custom_column( $column_name, $post_id ) { switch ( $column_name ) { case 'enabled': $disabled = get_post_meta( $post_id, self::DISABLE_META_KEY, true ); printf( '
', esc_attr( (string) $post_id ), esc_attr( wp_create_nonce( 'ata_toggle_snippet_' . $post_id ) ) ); wp_localize_script( 'ata-admin-snippets', 'ataSnippetData_' . $post_id, array( 'disabled' => ! empty( $disabled ), 'ajaxurl' => admin_url( 'admin-ajax.php' ), ) ); break; case 'type': $styles = Functions::get_snippet_type_styles( get_post( $post_id ) ); $snippet_type = $styles['type']; if ( ! get_post_meta( $post_id, '_ata_snippet_type', true ) ) { update_post_meta( $post_id, '_ata_snippet_type', $snippet_type ); } $url = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : admin_url( 'edit.php?post_type=ata_snippets' ); $url = add_query_arg( 'type', $snippet_type, $url ); printf( '%5$s', esc_url( $url ), esc_attr( $snippet_type ), esc_attr( $styles['background'] ), esc_attr( $styles['color'] ), esc_html( strtoupper( $snippet_type ) ) ); break; case 'shortcode': $shortcode = "[ata_snippet id={$post_id}]"; $output = sprintf( '', esc_attr( $shortcode ), __( 'Copy to clipboard', 'add-to-all' ) ); echo trim( $output, " \t\n\r\0\x0B" ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped break; case 'external_file': $snippet_type = Functions::get_snippet_type( get_post( $post_id ) ); if ( ! in_array( $snippet_type, array( 'css', 'js' ), true ) ) { echo '—'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped break; } $file_url = Functions::get_snippet_file_url( $post_id, $snippet_type ); $type_label = ( 'css' === $snippet_type ) ? 'CSS' : 'JS'; if ( $file_url ) { $link_text = sprintf( /* translators: %s: Snippet type (CSS or JS). */ __( 'View %s file', 'add-to-all' ), $type_label ); $link_label = sprintf( /* translators: 1: Snippet type (CSS or JS). 2: Snippet title. */ __( 'View %1$s file for %2$s', 'add-to-all' ), $type_label, get_the_title( $post_id ) ); printf( '%3$s', esc_url( $file_url ), esc_attr( $link_label ), esc_html( $link_text ) ); } else { echo esc_html__( 'File not generated', 'add-to-all' ); } break; default: break; } } /** * Order by or filtery by type meta value. * * @param \WP_Query $query The current query object. */ public function pre_get_posts( $query ) { // Check if we are on the admin screen for ata_snippets and sorting by type column. if ( is_admin() && 'ata_snippets' === $query->get( 'post_type' ) ) { if ( 'type' === $query->get( 'orderby' ) ) { $query->set( 'meta_key', '_ata_snippet_type' ); $query->set( 'orderby', 'meta_value' ); } } // Filter by type if type is set in the query. if ( isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $meta_query = array( array( 'key' => '_ata_snippet_type', 'value' => strtolower( sanitize_text_field( wp_unslash( $_GET['type'] ) ) ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended ), ); // Add the meta query to the main query object. $query->set( 'meta_query', $meta_query ); } } /** * Enqueue admin scripts. */ public function admin_scripts() { $minimize = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; $screen = get_current_screen(); if ( ! $screen || 'ata_snippets' !== $screen->post_type ) { return; } wp_register_style( 'ata-admin-columns', false, array(), WZ_SNIPPETZ_VERSION ); wp_enqueue_style( 'ata-admin-columns' ); wp_add_inline_style( 'ata-admin-columns', '.column-enabled { width: 60px; } .column-type { width: 90px; } .column-shortcode { width: 200px; } .ata_shortcode input.code { min-width: 100%; } .ata-column-narrow { display: block; } .ata-snippet-toggle-wrapper { text-align: center; }' ); if ( 'edit-ata_snippets' === $screen->id ) { wp_enqueue_script( 'ata-admin-snippets', plugins_url( 'includes/snippets/js/admin-snippets' . $minimize . '.js', WZ_SNIPPETZ_FILE ), array( 'wp-element', 'wp-i18n', 'wp-components' ), WZ_SNIPPETZ_VERSION, true ); wp_enqueue_style( 'wp-components' ); } } /** * Toggle snippet status via AJAX. */ public function ajax_toggle_snippet() { if ( ! isset( $_POST['post_id'] ) ) { wp_send_json_error( array( 'message' => __( 'No post ID provided.', 'add-to-all' ) ) ); } $post_id = absint( wp_unslash( $_POST['post_id'] ) ); check_ajax_referer( 'ata_toggle_snippet_' . $post_id, 'nonce' ); if ( ! current_user_can( 'edit_post', $post_id ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to edit this snippet.', 'add-to-all' ) ) ); } $disabled = get_post_meta( $post_id, self::DISABLE_META_KEY, true ); $new_status = ! $disabled; update_post_meta( $post_id, self::DISABLE_META_KEY, $new_status ); wp_send_json_success( array( 'disabled' => $new_status, ) ); } }