Plugin Directory

Changeset 2231084


Ignore:
Timestamp:
01/21/2020 05:12:04 PM (6 years ago)
Author:
cdecou
Message:

feat: create admin option page and can show input search for custom taxonomies

Location:
wp-search-category-admin/trunk
Files:
5 added
3 edited

Legend:

Unmodified
Added
Removed
  • wp-search-category-admin/trunk/README.md

    r2122082 r2231084  
    33Tags: category, admin, post, search, box
    44Requires at least: 4.0
    5 Tested up to: 5.2
    6 Stable tag: 1.2
     5Tested up to: 5.0
     6Stable tag: 1.5
    77License: GPLv2 or later
    88License URI: https://www.gnu.org/licenses/gpl-2.0.html
    99
    10 Add a search input into a Post edit Page to add fast category for a Post.
     10Add a search input into a Post edit Page to add fast category for a Post and in quick edit.
    1111
    1212== Description ==
     
    2525== Changelog ==
    2626
     27= 1.5 =
     28* Can display search input for custom taxonomies
     29* Create an admin option page "WPSCA Option" to activate search input
     30
    2731= 1.2 =
    2832* Compatibility with the new editor in WordPress 5.0+
  • wp-search-category-admin/trunk/js/script.js

    r1989484 r2231084  
    11(function( $ ){
    2     console.log(wp);
    32    $(window).load(function(){
    43        adminSearch.init();
    54    });
     5
    66    var adminSearch = {
    7         isGutenberg: false,
     7        isQuickEditPage: false,
     8        listInputTaxononomiesId: 'wpsca_list_taxonomies',
     9        listInputTaxononomiesNameId: 'wpsca_list_taxonomies_name',
     10        injectInputEventName: 'wpsca-inject-search-input',
     11
    812        getNameSearchField: function(){
    913            return 'wpsca-search-field';
    10         },
    11         getNameSearchButton: function(){
    12             return 'wpsca-search-button';
    1314        },
    1415        init : function(){
     
    1819            };
    1920
     21            this.isGutenberg = (typeof wp.element !== 'undefined');
    2022            // Can edit in post edit page or in quick edit
    21             var isQuickEditPage = $('.quick-edit-row').length > 0;
     23            this.isQuickEditPage = $('.quick-edit-row').length > 0;
    2224
    23             if (isQuickEditPage === true) {
     25            if (document.getElementById(this.listInputTaxononomiesId).length === 0) {
     26                return;
     27            }
     28            if (this.isQuickEditPage === true) {
    2429                this.initQuickEdit();
    2530            } else {
    26                 this.initEditPage();
     31                if (!this.isGutenberg) {
     32                    this.initEditPage();
     33                }
    2734            }
    2835        },
    2936
    30         getSearchBox: function() {
    31             var nameSearchField = this.getNameSearchField();
    32             var nameButtonGo = this.getNameSearchButton();
    33             return '<input type="text" name="'+ nameSearchField +'" id="'+ nameSearchField +'" class="meta-box-search-field" placeholder="Search" />';
     37        getListTaxonomies: function() {
     38            return document.getElementById(this.listInputTaxononomiesId).value.split(',');
     39        },
     40
     41        getSearchBox: function(taxonomyName) {
     42            var nameSearchField = this.getNameSearchField() + taxonomyName;
     43            return '<input type="text" name="'+ nameSearchField +'" id="'+ nameSearchField +'" data-taxonomy="' + taxonomyName +'" class="meta-box-search-field" placeholder="Search" />';
    3444        },
    3545
    3646        initEditPage: function() {
    3747            var globalObject = this;
    38             var search_box = this.getSearchBox();
    39             var nameSearchField = this.getNameSearchField();
    40             setTimeout(function() {
    41                 // WordPress Guttenberg or version sup or equal to 5.0
    42                 globalObject.isGutenberg = $('.editor-post-taxonomies__hierarchical-terms-list').length > 0;
    43                 // Add search_box input
    44                 if (globalObject.isGutenberg) {
    45                     $('.editor-post-taxonomies__hierarchical-terms-list').before(search_box);
    46                 } else {
    47                     $('#category-tabs').before(search_box);
    48                 }
     48            // get list input taxonomies and init initial vars
     49            var listTaxonomies = this.getListTaxonomies();
    4950
    50                 globalObject.initSearch('autocomplete', $('#'+nameSearchField), false);
    51             }, 800);
     51            listTaxonomies.forEach(function (taxonomy) {
     52                var searchBox = globalObject.getSearchBox(taxonomy);
     53                var divIdToInject = 'taxonomy-' + taxonomy;
     54                var nameSearchField = globalObject.getNameSearchField() + taxonomy;
     55
     56                $('#' + divIdToInject).prepend(searchBox);
     57                globalObject.initSearch($('#'+ nameSearchField), false);
     58            });
    5259        },
    5360
    5461        initQuickEdit: function() {
    5562            var globalObject = this;
     63            var listTaxonomies = this.getListTaxonomies();
     64
    5665            $('.editinline').on('click', function(event) {
     66                $('.meta-box-search-field').remove();
    5767                setTimeout(function() {
    58                     var search_box = globalObject.getSearchBox();
    59                     var nameSearchField = globalObject.getNameSearchField();
    60                     $('.meta-box-search-field').remove();
    61                     $('.cat-checklist.category-checklist').before(search_box);
    62                     globalObject.initSearch('autocomplete', $('#'+nameSearchField), true);
     68                    listTaxonomies.forEach(function (taxonomy) {
     69                        var search_box = globalObject.getSearchBox(taxonomy);
     70                        var nameSearchField = globalObject.getNameSearchField() + taxonomy;
     71                        $('.' + taxonomy + '-checklist.cat-checklist').first().before(search_box); // first to not have duplicate id
     72                        globalObject.initSearch($('#'+nameSearchField), true);
     73                    });
    6374                },50);
    6475            });
     
    6677        },
    6778
    68         initSearch: function(method, element, isQuickEdit){
    69             switch(method){
    70                 case 'click':
    71                     this.initClick(element);
    72                     break;
    73                 case 'autocomplete':
    74                     this.initAutocomplete(element, isQuickEdit);
    75                     break;
    76             }
     79        initSearch: function(element, isQuickEdit){
     80            this.initAutocomplete(element, isQuickEdit);
    7781        },
    7882
     
    8589                var $el = $(this);
    8690                var s = $el.siblings('#'+nameField).val();
    87                 that.searchCat(s, $el);
     91                that.searchTaxonomyElement(s, $el);
    8892            });
    8993        },
     
    9397            element.keyup($.debounce(500, function(){
    9498                var s = $(this).val();
    95                 that.searchCat(s, element, isQuickEdit);
     99                that.searchTaxonomyElement(s, element, isQuickEdit);
    96100            }));
    97101        },
    98102
    99         searchCat: function(s, elementEvent, isQuickEdit){
     103        searchTaxonomyElement: function(s, elementEvent, isQuickEdit){
    100104            var parentClass = 'categorydiv';
    101             var categoryChecklistClass = 'categorychecklist';
     105            var taxonomyChecklistClass = 'categorychecklist';
    102106            var itemSelector = 'li';
    103107
    104108            if (this.isGutenberg) {
    105109                parentClass = 'components-panel__body';
    106                 categoryChecklistClass = 'editor-post-taxonomies__hierarchical-terms-list';
     110                taxonomyChecklistClass = 'editor-post-taxonomies__hierarchical-terms-list';
    107111                itemSelector = '.editor-post-taxonomies__hierarchical-terms-choice';
    108112            }
    109113            if (isQuickEdit) {
    110114                parentClass = 'inline-edit-col';
    111                 categoryChecklistClass = 'category-checklist';
     115                var taxonomy = $(elementEvent).data('taxonomy')
     116                taxonomyChecklistClass = taxonomy + '-checklist';
    112117            }
    113118
    114119            if ( $.trim(s) == "" ){
    115                 elementEvent.parents('.' + parentClass).first().find('.'+ categoryChecklistClass +' ' + itemSelector).show();
     120                elementEvent.parents('.' + parentClass).first().find('.'+ taxonomyChecklistClass +' ' + itemSelector).show();
    116121            } else {
    117                 var result = elementEvent.parents('.' + parentClass).first().find('.'+categoryChecklistClass+' ' + itemSelector + ':Contains("'+s+'")');
     122                var result = elementEvent.parents('.' + parentClass).first().find('.'+taxonomyChecklistClass+' ' + itemSelector + ':Contains("'+s+'")');
    118123
    119                 elementEvent.parents('.' + parentClass).first().find('.' + categoryChecklistClass + ' ' + itemSelector).hide();
     124                elementEvent.parents('.' + parentClass).first().find('.' + taxonomyChecklistClass + ' ' + itemSelector).hide();
    120125                result.each(function(){
    121126                    $(this).show();
     
    123128            }
    124129        }
    125 
    126 
    127130    };
    128131})( jQuery );
  • wp-search-category-admin/trunk/wp-search-cat-admin.php

    r1989487 r2231084  
    44Plugin Name: WP search category admin
    55Description: Search dynamically a category in the box category in admin
    6 Version: 1.2
     6Version: 1.5
    77Author: Clément Décou
    88Author URI: http://www.clement-decou.fr
    99*/
     10
     11require_once __DIR__ . '/admin/ConfigurationAdmin.php';
     12
     13// init admin
     14if (is_admin()) {
     15    new ConfigurationAdmin();
     16}
    1017
    1118function register_wpsca_custom_script($hook)
     
    2229    wp_enqueue_style('wpsca_style', plugin_dir_url( __FILE__ ).'css/style.css');
    2330}
     31add_action( 'admin_enqueue_scripts', 'register_wpsca_custom_script');
    2432
    25 add_action( 'admin_enqueue_scripts', 'register_wpsca_custom_script');
     33// Add list of taxonomy in input hidden in admin footer
     34function print_input_list_taxonomies() {
     35    $screen = get_current_screen();
     36    if ($screen->parent_base !== 'edit') {
     37        return;
     38    }
     39
     40    $pluginOption = ConfigurationAdmin::getOption();
     41    $listTax = array_keys($pluginOption[ConfigurationAdmin::$registerTaxSectionName]);
     42    echo '<input type="hidden" id="wpsca_list_taxonomies" value="' . implode(',', $listTax) . '">';
     43}
     44add_action('admin_footer', 'print_input_list_taxonomies');
Note: See TracChangeset for help on using the changeset viewer.