Plugin Directory

Changeset 3207255


Ignore:
Timestamp:
12/12/2024 11:39:16 PM (12 months ago)
Author:
tmatsuur
Message:

[Update] Posts filter multiselect:2.3.0 Column widths can now be adjusted.

Location:
posts-filter-multiselect/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • posts-filter-multiselect/trunk/posts-filter-multiselect.php

    r3043855 r3207255  
    55 Description: Each pull-down menu on the post list page will be changed from single selection to multiple selection. Also, tags and author filters will be added to allow you to narrow down your search.
    66 Author: tmatsuur
    7  Version: 2.2.1
     7 Version: 2.3.0
    88 Author URI: https://12net.jp/
    99Text Domain: posts-filter-multiselect
     
    3737     */
    3838    public function __construct() {
    39         register_activation_hook( __FILE__ , array( &$this , 'activation' ) );
    40         register_deactivation_hook( __FILE__ , array( &$this , 'deactivation' ) );
     39        register_activation_hook( __FILE__ , array( $this , 'activation' ) );
     40        register_deactivation_hook( __FILE__ , array( $this , 'deactivation' ) );
    4141
    4242        global $pagenow;
     
    4848        }
    4949        add_action( 'admin_init', array( $this, 'ajax_inline_save' ) ); // @since 2.2.1
     50        add_action( 'wp_ajax_save_post_type_columns_width', [ $this, 'ajax_save_post_type_columns_width' ], 10 );
    5051    }
    5152
     
    321322     */
    322323    public function ajax_inline_save() {
    323         if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'inline-save' === $_POST['action'] &&
     324        if ( defined( 'DOING_AJAX' ) && DOING_AJAX &&
     325            isset( $_POST['action'] ) && 'inline-save' === $_POST['action'] &&
    324326            isset( $_POST['post_ID'] ) && (int) $_POST['post_ID'] ) {
    325327            $post = get_post( $_POST['post_ID'], ARRAY_A );
     
    336338    }
    337339
     340    /**
     341     * Creates a suffix for nonce value.
     342     *
     343     * @return string
     344     */
     345    private function nonce_action_suffix() {
     346        return date( 'His-Ymd', filemtime( __FILE__ ) );
     347    }
     348
     349    /**
     350     * Creates a nonce value.
     351     *
     352     * @param int|string $action
     353     * @return string
     354     */
     355    private function create_nonce( $action ) {
     356        return wp_create_nonce( $action . $this->nonce_action_suffix() );
     357    }
     358
     359    /**
     360     * Update '{post_type}_columns_width' in user meta data.
     361     *
     362     * @see wp_ajax_save_post_type_columns_width action.
     363     */
     364    public function ajax_save_post_type_columns_width() {
     365        check_ajax_referer( 'save_post_type_columns_width' . $this->nonce_action_suffix() );
     366
     367        if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX &&
     368            isset( $_POST['post_type'] ) &&
     369            in_array( $_POST['post_type'], get_post_types( array( 'show_ui' => true ) ), true ) ) ) {
     370            wp_die( __( 'Invalid request.' ) );
     371        }
     372
     373        $post_type_object = get_post_type_object( $_POST['post_type'] );
     374        if ( ! ( $post_type_object &&
     375            current_user_can( $post_type_object->cap->edit_posts ) ) ) {
     376            wp_die( __( 'Invalid post type.' ) );
     377        }
     378
     379        $data = array();
     380        if ( isset( $_POST['width'] ) && is_array( $_POST['width'] ) ) {
     381            $data['post_type'] = sanitize_key( $_POST['post_type'] );
     382            $data['width'] = array_map( 'intval', $_POST['width'] );
     383            $user = wp_get_current_user();
     384            update_user_meta( $user->ID, $data['post_type'] . '_columns_width', $data['width'] );
     385        }
     386        wp_send_json_success( $data );
     387    }
     388
    338389    /**
    339390     * Handle the multiple selectable pull-down menus.
     
    342393     */
    343394    public function admin_footer() {
     395        global $typenow;
     396        $post_type = $typenow;
     397        $user = wp_get_current_user();
     398        $columns_width = get_user_meta( $user->ID, sanitize_key( $post_type ) . '_columns_width', true );
     399        if ( empty( $columns_width ) ) {
     400            $columns_width = array();
     401        }
    344402?>
    345 <script type="text/javascript">
     403<style id="posts-filter-multiselect-css">
     404.metabox-prefs label {
     405    line-height: 2.75;
     406}
     407.metabox-prefs label span.prefix-widthp {
     408    position: relative;
     409    display: inline-block;
     410    margin: 0 0 0 .5em;
     411}
     412.metabox-prefs label span.prefix-widthp::before {
     413    content: '%';
     414    position: absolute;
     415    right: .75em;
     416    top: 0;
     417}
     418.metabox-prefs label input.column-widthp {
     419    width: 4.75em;
     420    padding-right: 1.5em;
     421}
     422</style>
     423<script id="posts-filter-multiselect-js" type="text/javascript">
    346424//<![CDATA[
    347425( function ( $ ) {
     426    const columns_width = { <?php
     427    if ( is_array( $columns_width ) ) {
     428        foreach ( $columns_width as $key => $value ) {
     429            printf( "'%s':%d,", esc_js( $key ), esc_js( (int)$value ) );
     430        }
     431    }
     432?> };
     433    document.querySelectorAll( '.metabox-prefs:not(.view-mode) > label' ).forEach( ( column ) => {
     434        const column_name = column.querySelector( 'input' ).value;
     435        const widthp = document.createElement( 'input' );
     436        widthp.setAttribute( 'type', 'number' );
     437        widthp.setAttribute( 'min', 1 );
     438        widthp.setAttribute( 'max', 50 );
     439        if ( columns_width[column_name] && 0 < columns_width[column_name] ) {
     440            widthp.setAttribute( 'value', columns_width[column_name] );
     441            document.getElementById( column_name ).style.width = columns_width[column_name] + '%';
     442        } else {
     443            widthp.setAttribute( 'value', '' );
     444        }
     445        widthp.classList.add( 'column-widthp' );
     446        widthp.dataset.column = column_name;
     447        const span = document.createElement( 'span' );
     448        span.classList.add( 'prefix-widthp' );
     449        span.append( widthp );
     450        column.append( span );
     451        widthp.addEventListener( 'change', ( event ) => {
     452            document.getElementById( event.target.dataset.column ).style.width = event.target.value + '%';
     453            save_columns_width();
     454        } );
     455        widthp.addEventListener( 'input', ( event ) => {
     456            if ( '' === event.target.value ) {
     457                document.getElementById( event.target.dataset.column ).style.removeProperty( 'width' );
     458                save_columns_width();
     459            }
     460        } );
     461    } );
     462    let timeoutID = null;
     463    function save_columns_width() {
     464        if ( null !== timeoutID ) {
     465            clearTimeout( timeoutID );
     466        }
     467        timeoutID = setTimeout( () => {
     468            timeoutID = null;
     469            const params = new URLSearchParams();
     470            params.append( 'action', 'save_post_type_columns_width' );
     471            params.append( '_ajax_nonce', '<?php echo esc_js( $this->create_nonce( 'save_post_type_columns_width' ) ); ?>' );
     472            params.append( 'post_type', '<?php echo esc_js( $post_type ); ?>' );
     473            document.querySelectorAll( '.column-widthp' ).forEach( ( widthp ) => {
     474                params.append( 'width[' + widthp.dataset.column + ']', widthp.value );
     475            } );
     476            fetch( ajaxurl, {
     477                method:      'POST',
     478                cache:       'no-cache',
     479                credentials: 'same-origin',
     480                body:        params
     481            } )
     482            .then( ( response ) => response.json() )
     483            .then( ( data ) => {} );
     484        }, 1000 );
     485    }
     486
    348487<?php if ( ! $this->_use_multiselect_js() ) { ?>
    349488    document.addEventListener('DOMContentLoaded', function() {
    350         var get_filter = JSON.parse( '<?php echo json_encode( $this->get_params ); ?>' );
     489        let get_filter = JSON.parse( '<?php echo json_encode( $this->get_params ); ?>' );
    351490        $( '#posts-filter input[name=filter_action]' ).siblings( 'select' ).each( function () {
    352491            first_text = $(this).find(':first' ).text();
    353             var multiselect_option = [];
     492            let multiselect_option = [];
    354493            $(this).find( 'option' ).each( function () {
    355494                multiselect_option.push( {value: $(this).attr( 'value' ), text: $(this).text(), checked: $(this).prop( 'selected' )} );
  • posts-filter-multiselect/trunk/readme.txt

    r3123175 r3207255  
    44Tags: posts
    55Requires at least: 4.1.0
    6 Tested up to: 6.6.0
    7 Stable Tag: 2.2.1
     6Tested up to: 6.7.1
     7Stable tag: 2.3.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6464
    6565== Changelog ==
     66
     67= 2.3.0 =
     68* Column widths can now be adjusted.
     69
     70= 2.2.2 =
     71* Fix - Several warning errors were resolved.
    6672
    6773= 2.2.1 =
Note: See TracChangeset for help on using the changeset viewer.