Plugin Directory

Changeset 3776355


Ignore:
Timestamp:
10/31/2026 05:43:48 AM (18 hours ago)
Author:
fahadmahmood
Message:

releasing 1.3.7

Location:
wp-sort-order
Files:
53 added
11 edited

Legend:

Unmodified
Added
Removed
  • wp-sort-order/trunk/inc/functions.php

    r3682718 r3776355  
    1 <?php if ( ! defined( 'ABSPATH' ) ) exit;
    2                
    3         function sanitize_wpso_data( $input ) {
    4                 if(is_array($input)){           
    5                         $new_input = array();   
    6                         foreach ( $input as $key => $val ) {
    7                                 $new_input[ $key ] = (is_array($val)?sanitize_wpso_data($val):sanitize_text_field( $val ));
    8                         }                       
    9                 }else{
    10                         $new_input = sanitize_text_field($input);                       
    11                         if(stripos($new_input, '@') && is_email($new_input)){
    12                                 $new_input = sanitize_email($new_input);
     1<?php
     2if ( ! defined( 'ABSPATH' ) ) {
     3        exit;
     4}
     5
     6/**
     7 * Is the optional /pro add-on present and loaded?
     8 *
     9 * @return bool
     10 */
     11function wpso_is_pro() {
     12        return ! empty( $GLOBALS['wpso_pro'] );
     13}
     14
     15/**
     16 * Recursively sanitize plugin data.
     17 *
     18 * @param mixed $input Raw value.
     19 * @return mixed
     20 */
     21function sanitize_wpso_data( $input ) {
     22        if ( is_array( $input ) ) {
     23                $new_input = array();
     24                foreach ( $input as $key => $val ) {
     25                        $key               = is_int( $key ) ? $key : sanitize_text_field( (string) $key );
     26                        $new_input[ $key ] = is_array( $val ) ? sanitize_wpso_data( $val ) : sanitize_text_field( (string) $val );
     27                }
     28                return $new_input;
     29        }
     30
     31        $new_input = sanitize_text_field( (string) $input );
     32        if ( false !== stripos( $new_input, '@' ) && is_email( $new_input ) ) {
     33                $new_input = sanitize_email( $new_input );
     34        }
     35        if ( 0 === stripos( $new_input, 'http' ) && wp_http_validate_url( $new_input ) ) {
     36                $new_input = sanitize_url( $new_input );
     37        }
     38        return $new_input;
     39}
     40
     41/**
     42 * Who may drag & drop?  Administrators by default; the pro add-on may extend this
     43 * (never to anything the user could not already edit - every handler also checks
     44 * per-object capabilities).
     45 *
     46 * @return bool
     47 */
     48function wpso_user_can_sort() {
     49        return (bool) apply_filters( 'wpso_user_can_sort', current_user_can( 'manage_options' ) );
     50}
     51
     52/**
     53 * Read a sanitized value from $_GET.
     54 *
     55 * @param string $key     Query var.
     56 * @param string $default Default.
     57 * @return string
     58 */
     59function wpso_get( $key, $default = '' ) {
     60        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only routing, no state change.
     61        return isset( $_GET[ $key ] ) && ! is_array( $_GET[ $key ] ) ? sanitize_text_field( wp_unslash( $_GET[ $key ] ) ) : $default;
     62}
     63
     64/**
     65 * Current admin request path (sanitized).
     66 *
     67 * @return string
     68 */
     69function wpso_request_uri() {
     70        return isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
     71}
     72
     73function wp_wpso_enqueue_scripts() {
     74        global $wpdb;
     75
     76        wp_enqueue_script(
     77                'wpso-front-scripts',
     78                plugins_url( 'js/front-scripts.js', WPSO_FILE ),
     79                array( 'jquery' ),
     80                WPSO_VERSION,
     81                true
     82        );
     83
     84        $wp_wpso_array = array(
     85                'ajaxurl' => admin_url( 'admin-ajax.php' ),
     86        );
     87
     88        $expected_items = array();
     89
     90        // "User Categories" is a premium taxonomy - nothing to look up without it.
     91        if ( wpso_is_pro() && taxonomy_exists( 'usercategories' ) ) {
     92                $expected_terms = $wpdb->get_results(
     93                        $wpdb->prepare( "SELECT t.term_id FROM {$wpdb->term_taxonomy} t WHERE t.taxonomy = %s", 'usercategories' )
     94                );
     95
     96                foreach ( (array) $expected_terms as $eterms ) {
     97                        $tax_term = $wpdb->get_row(
     98                                $wpdb->prepare(
     99                                        "SELECT t.taxonomy, t.term_id FROM {$wpdb->term_taxonomy} t, {$wpdb->terms} tr WHERE tr.term_id = %d AND tr.term_id = t.term_id LIMIT 1",
     100                                        $eterms->term_id
     101                                )
     102                        );
     103                        if ( ! $tax_term ) {
     104                                continue;
    13105                        }
    14                         if(stripos($new_input, 'http') || wp_http_validate_url($new_input)){
    15                                 $new_input = sanitize_url($new_input);
    16                         }                       
    17                 }       
    18                 return $new_input;
    19         }       
    20         function wp_wpso_enqueue_scripts() {
    21                
    22                 global $wpdb;
    23                
    24                 wp_enqueue_script(
    25                         'wpso-front-scripts',
    26                         plugins_url('js/front-scripts.js', dirname(__FILE__)),
    27                         array('jquery'),
    28                         date('Ymhi')
    29                 );
    30                
    31                 $expected_terms = $wpdb->get_results("SELECT t.term_id FROM ".$wpdb->prefix."term_taxonomy t WHERE t.taxonomy='usercategories'");               
    32                
    33                 $wp_wpso_array = array(
    34                                                                 'ajaxurl' => admin_url( 'admin-ajax.php' ),     
    35                                                                 'nonce'=> wp_create_nonce('wpso_nonce_ensured'),
    36                                                 );
    37                                                
    38                 $expected_items = array();                             
    39                                                
    40                 if(!empty($expected_terms)){
    41                         foreach($expected_terms as $eterms){
    42                                 //pree($eterms);
    43                                
    44                                 //$tquery = "SELECT t.taxonomy, t.term_id FROM $wpdb->term_taxonomy t, $wpdb->terms tr WHERE ".("tr.term_id='".$eterms->term_id."'")." AND tr.term_id=t.term_id LIMIT 1";       
    45                                 //pree($tquery);
    46                                 //$tax_term = $wpdb->get_row($tquery);
    47                                
    48                                 $tquery = $wpdb->prepare(
    49                                         "SELECT t.taxonomy, t.term_id FROM $wpdb->term_taxonomy t, $wpdb->terms tr WHERE tr.term_id = %d AND tr.term_id = t.term_id LIMIT 1",
    50                                         $eterms->term_id
     106
     107                        $taxonomy   = $tax_term->taxonomy;
     108                        $term_id    = (int) $tax_term->term_id;
     109                        $term_items = get_term_children( $term_id, $taxonomy );
     110
     111                        if ( empty( $term_items ) || is_wp_error( $term_items ) ) {
     112                                $term_items_direct = get_objects_in_term( $term_id, $taxonomy );
     113                                $term_items        = ( ! empty( $term_items_direct ) && ! is_wp_error( $term_items_direct ) ) ? array( $term_id ) : array();
     114                        }
     115
     116                        foreach ( $term_items as $items ) {
     117                                $items        = (int) $items;
     118                                $team_members = $wpdb->get_results(
     119                                        $wpdb->prepare(
     120                                                "SELECT um.user_id FROM {$wpdb->usermeta} um WHERE um.meta_key = %s ORDER BY CAST(um.meta_value AS unsigned) ASC",
     121                                                'user_order_' . $items
     122                                        )
    51123                                );
    52                                 $tax_term = $wpdb->get_row($tquery);
    53                                
    54                                
    55                                 $taxonomy = $tax_term->taxonomy;
    56                                 $term_id = $tax_term->term_id;
    57                                 $term_items = get_term_children($term_id, $taxonomy);
    58                                
    59                                 if(empty($term_items)){
    60                                         $term_items_direct = get_objects_in_term( $term_id, $taxonomy); //07-10-2018
    61                                         if(!empty($term_items_direct)){
    62                                                 $term_items = array($term_id);
    63                                         }
     124
     125                                foreach ( (array) $team_members as $team_member ) {
     126                                        $expected_items[ $items ][] = array(
     127                                                'user_id'   => (int) $team_member->user_id,
     128                                                'user_link' => esc_url_raw( get_author_posts_url( $team_member->user_id ) ),
     129                                        );
    64130                                }
    65                                
    66                                 if(!empty($term_items)){
    67                                        
    68                                         if($items>0)
    69                                         $expected_items[$items] = array();
    70                                        
    71                                         foreach($term_items as $items){
    72                                                 //pree($items);
    73                                                 $term = get_term_by('id', $items, $taxonomy);
    74                                                
    75                                                
    76                                                 /*$squery = "
    77                                                                                 SELECT                                                 
    78                                                                                         um.user_id
    79                                                                                        
    80                                                                                 FROM
    81                                                                                         $wpdb->usermeta um
    82                                                                                 WHERE                                                                           
    83                                                                                         um.meta_key=CONCAT('user_order_', $items)
    84                                                                                 ORDER BY
    85                                                                                         CAST(um.meta_value AS unsigned)
    86                                                                                 ASC
    87                                                                                        
    88                                                                         ";
    89                                                                        
    90                                                 $teamMembers = $wpdb->get_results($squery);*/
    91                                                
    92                                                 $squery = $wpdb->prepare(
    93                                                         "SELECT um.user_id
    94                                                         FROM $wpdb->usermeta um
    95                                                         WHERE um.meta_key = %s
    96                                                         ORDER BY CAST(um.meta_value AS unsigned) ASC",
    97                                                         'user_order_' . $items
    98                                                 );
    99                                                 $teamMembers = $wpdb->get_results($squery);
    100                                                
    101                                                
    102                                                 if(!empty($teamMembers)){                                                       
    103                                                         foreach ( $teamMembers as $teamMember ){
    104                                                                        
    105                                                                 //$teamMember_info = get_userdata($teamMember->user_id);       
    106                                                                 //pree(get_author_posts_url($teamMember->user_id));
    107                                                                 $expected_items[$items][] = array('user_id'=>$teamMember->user_id, 'user_link'=>get_author_posts_url($teamMember->user_id));
    108                                                         }
    109                                                        
    110                                                 }
    111                                         }
    112                                 }
    113 
    114131                        }
    115                 }               
    116                
    117                 $wp_wpso_array['user_items'] = $expected_items;
    118                
    119                 wp_localize_script( 'wpso-front-scripts', 'wpso', $wp_wpso_array );
    120         }
    121        
    122         add_action( 'wp_enqueue_scripts', 'wp_wpso_enqueue_scripts', 99 );     
    123        
    124        
    125         function wp_wpso_admin_enqueue_scripts() {
    126                
    127                 global $wpdb;
    128                
    129                 if(isset($_GET['page']) && $_GET['page']=='wpso-settings'){
    130                        
    131                         wp_enqueue_script( 'wpso-fontawesome', plugin_dir_url( dirname(__FILE__) ) . 'js/fontawesome.min.js' );
    132                         wp_enqueue_style( 'wpso-fontawesome', plugins_url('css/fontawesome.min.css', dirname(__FILE__)), array());     
    133                         wp_enqueue_script( 'wpso-bootstrap', plugin_dir_url( dirname(__FILE__) ) . 'js/bootstrap.min.js' );
    134                         wp_enqueue_style( 'wpso-bootstrap', plugins_url('css/bootstrap.min.css', dirname(__FILE__)), array());                         
    135                 }
    136                
    137         }
    138                        
    139         add_action( 'admin_enqueue_scripts', 'wp_wpso_admin_enqueue_scripts', 99 );             
    140        
    141         if(!function_exists('pre')){
    142                 function pre($data){
    143                         if(isset($_GET['debug']) && current_user_can('manage_options')){
    144                                 pree($data);
    145                         }
    146                 }       
    147         }
    148                
    149         if(!function_exists('pree')){
    150         function pree($data){
    151                                 if ( ! current_user_can( 'manage_options' ) ) {
    152                                         return;
    153                                 }
    154                                 echo '<pre>';
    155                                 print_r($data);
    156                                 echo '</pre>'; 
    157                
    158                 }       
    159         }       
    160 
    161         if ( ! function_exists("wpso_plugin_links"))
    162         {
    163                 function wpso_plugin_links($links) {
    164                         global $wpso_premium_link, $wpso_pro;
    165                        
    166                         $settings_link = '<a href="options-general.php?page=wpso-settings">'.__('Settings', 'wpso-sort-order').'</a>';
    167                        
    168                         if($wpso_pro){
    169                                 array_unshift($links, $settings_link);
    170                         }else{
    171                                  
    172                                 $wpso_premium_link = '<a href="'.esc_url($wpso_premium_link).'" title="'.__('Go Premium', 'wpso-sort-order').'" target=_blank>'.__('Go Premium', 'wpso-sort-order').'</a>';
    173                                 array_unshift($links, $settings_link, $wpso_premium_link);
    174                        
    175                         }
    176                        
    177                        
    178                         return $links;
    179                 }
    180         }
    181        
    182 /**
    183 * Uninstall hook
    184 */
    185        
    186         register_uninstall_hook( __FILE__, 'wpso_uninstall' );
    187        
    188         function wpso_uninstall()
    189         {
    190                 global $wpdb;
    191                 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
    192                         $curr_blog = $wpdb->blogid;
    193                         $blogids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
    194                         foreach( $blogids as $blog_id ) {
    195                                 switch_to_blog( $blog_id );
    196                                 wpso_uninstall_db();
    197                         }
    198                         switch_to_blog( $curr_blog );
     132                }
     133        }
     134
     135        $wp_wpso_array['user_items'] = $expected_items;
     136
     137        wp_localize_script( 'wpso-front-scripts', 'wpso', $wp_wpso_array );
     138}
     139add_action( 'wp_enqueue_scripts', 'wp_wpso_enqueue_scripts', 99 );
     140
     141function wp_wpso_admin_enqueue_scripts() {
     142        if ( 'wpso-settings' === wpso_get( 'page' ) && current_user_can( 'manage_options' ) ) {
     143                wp_enqueue_script( 'wpso-fontawesome', WPSO_URL . '/js/fontawesome.min.js', array(), WPSO_VERSION, true );
     144                wp_enqueue_style( 'wpso-fontawesome', WPSO_URL . '/css/fontawesome.min.css', array(), WPSO_VERSION );
     145                wp_enqueue_script( 'wpso-bootstrap', WPSO_URL . '/js/bootstrap.min.js', array( 'jquery' ), WPSO_VERSION, true );
     146                wp_enqueue_style( 'wpso-bootstrap', WPSO_URL . '/css/bootstrap.min.css', array(), WPSO_VERSION );
     147        }
     148}
     149add_action( 'admin_enqueue_scripts', 'wp_wpso_admin_enqueue_scripts', 99 );
     150
     151if ( ! function_exists( 'pre' ) ) {
     152        function pre( $data ) {
     153                if ( isset( $_GET['debug'] ) && current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     154                        pree( $data );
     155                }
     156        }
     157}
     158
     159if ( ! function_exists( 'pree' ) ) {
     160        function pree( $data ) {
     161                if ( ! current_user_can( 'manage_options' ) ) {
     162                        return;
     163                }
     164                echo '<pre>' . esc_html( print_r( $data, true ) ) . '</pre>'; // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
     165        }
     166}
     167
     168if ( ! function_exists( 'wpso_plugin_links' ) ) {
     169        function wpso_plugin_links( $links ) {
     170                global $wpso_premium_link;
     171
     172                $settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=wpso-settings' ) ) . '">' . esc_html__( 'Settings', 'wpso-sort-order' ) . '</a>';
     173
     174                if ( wpso_is_pro() ) {
     175                        array_unshift( $links, $settings_link );
    199176                } else {
    200                         wpso_uninstall_db();
    201                 }
    202         }
    203         function wpso_uninstall_db()
    204         {
    205                 global $wpdb;
    206                 $result = $wpdb->query( "DESCRIBE $wpdb->terms `term_order`" );
    207                 if ( $result ){
    208                         $query = "ALTER TABLE $wpdb->terms DROP `term_order`";
    209                         $result = $wpdb->query( $query );
    210                 }
    211                
    212                 $result = $wpdb->query( "DESCRIBE $wpdb->users `user_order`" );
    213                 if ( $result ){
    214                         $query = "ALTER TABLE $wpdb->users DROP `user_order`";
    215                         $result = $wpdb->query( $query );
    216                 }
    217        
    218                 delete_option( 'wpso_activation' );     
    219         }
    220         add_action('admin_init', 'wpso_admin_init');
    221         function wpso_admin_init(){
    222                 //pree($_GET);
    223                 if(isset($_GET['get_keys'])){
    224                         if( ! current_user_can( 'manage_options' ) ) {
    225                                 wp_die( esc_html__( 'Unauthorized access.', 'wpso-sort-order' ) );
    226                         }
    227                         if(isset($_GET['post']) && is_numeric($_GET['post'])){
    228                                 pre(get_post($_GET['post']));
    229                                 pre(get_post_meta($_GET['post']));
    230                                 wp_die();
    231                         }
    232                 }
    233         }
     177                        $go_premium = '<a href="' . esc_url( $wpso_premium_link ) . '" title="' . esc_attr__( 'Go Premium', 'wpso-sort-order' ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Go Premium', 'wpso-sort-order' ) . '</a>';
     178                        array_unshift( $links, $settings_link, $go_premium );
     179                }
     180
     181                return $links;
     182        }
     183}
     184
     185/**
     186 * Debug helper (administrators only): ?get_keys&post=ID dumps a post and its meta.
     187 */
     188function wpso_admin_init() {
     189        if ( isset( $_GET['get_keys'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     190                if ( ! current_user_can( 'manage_options' ) ) {
     191                        wp_die( esc_html__( 'Unauthorized access.', 'wpso-sort-order' ) );
     192                }
     193                $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     194                if ( $post_id ) {
     195                        pre( get_post( $post_id ) );
     196                        pre( get_post_meta( $post_id ) );
     197                        wp_die();
     198                }
     199        }
     200}
     201add_action( 'admin_init', 'wpso_admin_init' );
  • wp-sort-order/trunk/inc/hooks.php

    r3682718 r3776355  
    1 <?php if ( ! defined( 'ABSPATH' ) ) exit;
     1<?php
     2if ( ! defined( 'ABSPATH' ) ) {
     3        exit;
     4}
     5
    26/**
    3 * Class & Method
    4 */
    5        
    6         $wpso = new Wpso();
    7        
    8         class Wpso
    9         {
    10                 function __construct()
    11                 {
    12                         if ( !get_option( 'wpso_activation' ) ) $this->wpso_activation();
    13                        
    14                         add_action( 'plugins_loaded', array( $this, 'wpso_load_textdomain' ) );
    15        
    16                         add_action( 'admin_menu', array( $this, 'admin_menu') );
    17                        
    18                         if(isset($_GET['page']) && $_GET['page']=='wpso-settings')
     7 * Class & Method
     8 */
     9class Wpso {
     10
     11        /**
     12         * Post statuses that take part in ordering.
     13         */
     14        const STATUSES = "'publish', 'pending', 'draft', 'private', 'future'";
     15
     16        public function __construct() {
     17                if ( ! get_option( 'wpso_activation' ) ) {
     18                        $this->wpso_activation();
     19                }
     20
     21                add_action( 'admin_menu', array( $this, 'admin_menu' ) );
     22
     23                if ( 'wpso-settings' === wpso_get( 'page' ) ) {
    1924                        add_action( 'admin_init', array( $this, 'refresh' ) );
    20                        
    21        
    22                        
    23                         add_action( 'admin_init', array( $this, 'update_options') );
    24                         add_action( 'admin_init', array( $this, 'update_options_premium') );
    25                         add_action( 'admin_init', array( $this, 'load_script_css' ) );
    26                        
    27                         // sortable ajax action
    28                         add_action( 'wp_ajax_update-menu-order', array( $this, 'update_menu_order' ) );
    29                         add_action( 'wp_ajax_update-menu-order-tags', array( $this, 'update_menu_order_tags' ) );
    30                        
    31                         add_action( 'wp_ajax_update-menu-order-users', array( $this, 'update_menu_order_users' ) );
    32                         add_action( 'wp_ajax_update-menu-order-extras', array( $this, 'update_menu_order_extras' ) );
    33                        
    34                        
    35                         // reorder post types
    36                         add_action( 'pre_get_posts', array( $this, 'wpso_pre_get_posts' ) );
    37                        
    38                         add_filter( 'get_previous_post_where', array( $this, 'wpso_previous_post_where' ) );
    39                         add_filter( 'get_previous_post_sort', array( $this, 'wpso_previous_post_sort' ) );
    40                         add_filter( 'get_next_post_where', array( $this, 'hocpo_next_post_where' ) );
    41                         add_filter( 'get_next_post_sort', array( $this, 'wpso_next_post_sort' ) );
    42                        
    43                         // reorder taxonomies
    44                         add_filter( 'get_terms_orderby', array( $this, 'wpso_get_terms_orderby' ), 10, 3 );
    45                         add_filter( 'wp_get_object_terms', array( $this, 'wpso_get_object_terms' ), 10, 3 );
    46                         add_filter( 'get_terms', array( $this, 'wpso_get_object_terms' ), 10, 3 );
    47                        
    48                         // reorder users
    49                         add_action('pre_user_query', array($this, 'wpso_pre_user_query'));
    50                 }
    51                
    52                 function wpso_pre_user_query($userquery){
    53                        
    54                         global $wpdb;
    55                        
    56                         $screen = get_current_screen();
    57                        
    58                         if ( ! is_object( $screen ) or 'users' != $screen->id )
     25                }
     26
     27                add_action( 'admin_init', array( $this, 'update_options' ) );
     28                add_action( 'admin_init', array( $this, 'update_options_premium' ) );
     29                add_action( 'admin_init', array( $this, 'load_script_css' ) );
     30
     31                // sortable ajax actions
     32                add_action( 'wp_ajax_update-menu-order', array( $this, 'update_menu_order' ) );
     33                add_action( 'wp_ajax_update-menu-order-tags', array( $this, 'update_menu_order_tags' ) );
     34                add_action( 'wp_ajax_update-menu-order-users', array( $this, 'update_menu_order_users' ) );
     35                add_action( 'wp_ajax_update-menu-order-extras', array( $this, 'update_menu_order_extras' ) );
     36
     37                // reorder post types
     38                add_action( 'pre_get_posts', array( $this, 'wpso_pre_get_posts' ) );
     39
     40                add_filter( 'get_previous_post_where', array( $this, 'wpso_previous_post_where' ) );
     41                add_filter( 'get_previous_post_sort', array( $this, 'wpso_previous_post_sort' ) );
     42                add_filter( 'get_next_post_where', array( $this, 'hocpo_next_post_where' ) );
     43                add_filter( 'get_next_post_sort', array( $this, 'wpso_next_post_sort' ) );
     44
     45                // reorder taxonomies
     46                add_filter( 'get_terms_orderby', array( $this, 'wpso_get_terms_orderby' ), 10, 3 );
     47                add_filter( 'wp_get_object_terms', array( $this, 'wpso_get_object_terms' ), 10, 3 );
     48                add_filter( 'get_terms', array( $this, 'wpso_get_object_terms' ), 10, 3 );
     49
     50                // reorder users
     51                add_action( 'pre_user_query', array( $this, 'wpso_pre_user_query' ) );
     52        }
     53
     54        /* ------------------------------------------------------------------ *
     55         * Users
     56         * ------------------------------------------------------------------ */
     57
     58        public function wpso_pre_user_query( $userquery ) {
     59                global $wpdb;
     60
     61                if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
    5962                        return;
    60                
    61                         $vars = $userquery->query_vars;
    62                        
    63                         //pree($vars);
    64                        
    65                         $valid_tax = $this->current_tax($_GET);
    66                        
    67                         //pree($valid_tax);
    68                         if($valid_tax==0)
     63                }
     64
     65                $screen = get_current_screen();
     66                if ( ! is_object( $screen ) || 'users' !== $screen->id ) {
    6967                        return;
    70                        
    71                         $userquery->query_from .= " LEFT JOIN {$wpdb->usermeta} m1 ON {$wpdb->users}.ID=m1.user_id AND (m1.meta_key='user_order_".$valid_tax."')";
    72                        
    73                         $userquery->query_orderby = " ORDER BY CAST(m1.meta_value AS unsigned) ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");
    74                        
    75                         //pree($userquery);
    76                 }
    77                
    78                 function wpso_activation()
    79                 {
    80                         global $wpdb;
    81                         $result = $wpdb->query( "DESCRIBE $wpdb->terms `term_order`" );
    82 
    83                         if ( !$result ) {
    84                                 $query = "ALTER TABLE $wpdb->terms ADD `term_order` INT( 4 ) NULL DEFAULT '0'";
    85                                 $result = $wpdb->query( $query );
    86                                
    87                         }
    88        
    89                         $result = $wpdb->query( "DESCRIBE $wpdb->users `user_order`" );
    90                         if ( !$result ) {
    91                                 $query = "ALTER TABLE $wpdb->users ADD `user_order` INT( 4 ) NULL DEFAULT '0'";
    92                                 $result = $wpdb->query( $query );
    93                         }
    94        
    95                         update_option( 'wpso_activation', 1 );
    96                 }
    97        
    98                 function wpso_load_textdomain()
    99                 {
    100                         //load_plugin_textdomain( 'wp-sort-order', false, basename( dirname( __FILE__ ) ).'/languages/' );
    101                 }
    102                 function admin_menu()
    103                 {
    104                         add_options_page( 'WP Sort Order', 'WP Sort Order', 'manage_options', 'wpso-settings', array( $this,'admin_page' ) );
    105                 }
    106                
    107                 function admin_page()
    108                 {
    109                         require WPSO_DIR.'inc/settings.php';
    110                 }
    111        
    112                 function _check_load_script_css()
    113                 {
    114                         global $wpso_pro, $wpso_allowed_pages;
    115                         $active = false;
    116                        
    117                         $objects = $this->get_wpso_options_objects();
    118                         $tags = $this->get_wpso_options_tags();
    119                         $extras = $this->get_wpso_options_extras();
    120                         //pree($extras);
    121                        
    122                        
    123                         if (!$active && !empty( $extras ) ) {
    124                                 foreach($wpso_allowed_pages as $page_name=>$page_title){
    125                                         if(in_array($page_name, $extras) && (strstr( $_SERVER['REQUEST_URI'], 'wp-admin/'.$page_name ))){
    126                                                 $active = true;
     68                }
     69
     70                $valid_tax = absint( $this->current_tax( $_GET ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- sanitized in current_tax().
     71                if ( 0 === $valid_tax ) {
     72                        return;
     73                }
     74
     75                $userquery->query_from .= $wpdb->prepare(
     76                        " LEFT JOIN {$wpdb->usermeta} m1 ON {$wpdb->users}.ID = m1.user_id AND (m1.meta_key = %s)",
     77                        'user_order_' . $valid_tax
     78                );
     79
     80                $order                  = ( isset( $userquery->query_vars['order'] ) && 'ASC' === strtoupper( $userquery->query_vars['order'] ) ) ? 'asc' : 'desc';
     81                $userquery->query_orderby = ' ORDER BY CAST(m1.meta_value AS unsigned) ' . $order . ' ';
     82        }
     83
     84        /* ------------------------------------------------------------------ *
     85         * Activation / menu
     86         * ------------------------------------------------------------------ */
     87
     88        public function wpso_activation() {
     89                global $wpdb;
     90
     91                if ( ! $wpdb->query( "DESCRIBE {$wpdb->terms} `term_order`" ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery
     92                        $wpdb->query( "ALTER TABLE {$wpdb->terms} ADD `term_order` INT( 4 ) NULL DEFAULT '0'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
     93                }
     94
     95                if ( ! $wpdb->query( "DESCRIBE {$wpdb->users} `user_order`" ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery
     96                        $wpdb->query( "ALTER TABLE {$wpdb->users} ADD `user_order` INT( 4 ) NULL DEFAULT '0'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
     97                }
     98
     99                update_option( 'wpso_activation', 1 );
     100        }
     101
     102        public function wpso_load_textdomain() {
     103                // Kept for backward compatibility - translations are loaded on "init" from index.php.
     104        }
     105
     106        public function admin_menu() {
     107                add_options_page( 'WP Sort Order', 'WP Sort Order', 'manage_options', 'wpso-settings', array( $this, 'admin_page' ) );
     108        }
     109
     110        public function admin_page() {
     111                if ( ! current_user_can( 'manage_options' ) ) {
     112                        wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ), '', array( 'response' => 403 ) );
     113                }
     114                require WPSO_DIR . 'inc/settings.php';
     115        }
     116
     117        /* ------------------------------------------------------------------ *
     118         * Scripts
     119         * ------------------------------------------------------------------ */
     120
     121        public function _check_load_script_css() {
     122                global $wpso_allowed_pages;
     123
     124                $active  = false;
     125                $objects = $this->get_wpso_options_objects();
     126                $tags    = $this->get_wpso_options_tags();
     127                $extras  = $this->get_wpso_options_extras();
     128                $uri     = wpso_request_uri();
     129
     130                if ( ! empty( $extras ) && current_user_can( 'manage_options' ) ) {
     131                        foreach ( (array) $wpso_allowed_pages as $page_name => $page_title ) {
     132                                if ( in_array( $page_name, $extras, true ) && false !== strpos( $uri, 'wp-admin/' . $page_name ) ) {
     133                                        $active = true;
     134                                }
     135                        }
     136                }
     137
     138                if ( empty( $objects ) && empty( $tags ) ) {
     139                        return false;
     140                }
     141
     142                // exclude (sorting, addnew page, edit page)
     143                if ( isset( $_GET['orderby'] ) || false !== strpos( $uri, 'action=edit' ) || false !== strpos( $uri, 'wp-admin/post-new.php' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     144                        return false;
     145                }
     146
     147                $post_type = wpso_get( 'post_type' );
     148                $taxonomy  = wpso_get( 'taxonomy' );
     149
     150                if ( ! $active && ! empty( $objects ) ) {
     151                        if ( '' !== $post_type && '' === $taxonomy && in_array( $post_type, $objects, true ) ) { // if page or custom post types
     152                                $active = true;
     153                        }
     154                        if ( '' === $post_type && false !== strpos( $uri, 'wp-admin/edit.php' ) && in_array( 'post', $objects, true ) ) { // if post
     155                                $active = true;
     156                        }
     157                }
     158
     159                if ( ! $active && ! empty( $tags ) ) {
     160                        if ( '' !== $taxonomy && in_array( $taxonomy, $tags, true ) ) {
     161                                $active = true;
     162                        }
     163
     164                        foreach ( $tags as $tag ) {
     165                                if ( isset( $_GET[ $tag ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     166                                        $active = true;
     167                                        break;
     168                                }
     169                        }
     170
     171                        if ( wpso_is_pro() && false !== strpos( $uri, 'wp-admin/users.php' ) && in_array( 'usercategories', $tags, true ) ) {
     172                                $active = true;
     173                        }
     174                }
     175
     176                return $active;
     177        }
     178
     179        public function load_script_css() {
     180                $translation_array = array(
     181                        'ajaxurl'  => admin_url( 'admin-ajax.php' ),
     182                        'this_url' => admin_url( 'admin.php?page=wpso-settings' ),
     183                        'wpso_tab' => (string) absint( wpso_get( 't', '0' ) ),
     184                        'nonce'    => wp_create_nonce( 'wpso_nonce_ensured' ),
     185                );
     186
     187                if ( $this->_check_load_script_css() && wpso_user_can_sort() ) {
     188                        wp_enqueue_script( 'jquery' );
     189                        wp_enqueue_script( 'jquery-ui-sortable' );
     190                        wp_enqueue_script( 'wpsojs', WPSO_URL . '/js/scripts.js', array( 'jquery' ), WPSO_VERSION, true );
     191                        wp_enqueue_style( 'wpso', WPSO_URL . '/css/styles.css', array(), WPSO_VERSION );
     192                }
     193
     194                if ( is_admin() && 'wpso-settings' === wpso_get( 'page' ) && current_user_can( 'manage_options' ) ) {
     195                        wp_enqueue_style( 'wpso', WPSO_URL . '/css/admin.css', array(), WPSO_VERSION );
     196                        wp_enqueue_script( 'wpsojs', WPSO_URL . '/js/admin.js', array( 'jquery' ), WPSO_VERSION, true );
     197                }
     198
     199                if ( wp_script_is( 'wpsojs', 'enqueued' ) ) {
     200                        wp_localize_script( 'wpsojs', 'wpso_obj', $translation_array );
     201                }
     202        }
     203
     204        /* ------------------------------------------------------------------ *
     205         * Re-numbering helpers
     206         * ------------------------------------------------------------------ */
     207
     208        /**
     209         * Re-number menu_order of the given post types.
     210         *
     211         * @param array  $objects Post types.
     212         * @param string $mode    "keep" (current order), "page" (menu_order, title) or "date".
     213         */
     214        private function renumber_objects( $objects, $mode = 'keep' ) {
     215                global $wpdb;
     216
     217                foreach ( (array) $objects as $object ) {
     218                        $result = $wpdb->get_results(
     219                                $wpdb->prepare(
     220                                        'SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min FROM ' . $wpdb->posts . ' WHERE post_type = %s AND post_status IN (' . self::STATUSES . ')',
     221                                        $object
     222                                )
     223                        );
     224                        if ( empty( $result ) || 0 == $result[0]->cnt || $result[0]->cnt == $result[0]->max ) {
     225                                continue;
     226                        }
     227
     228                        if ( 'page' === $mode && 'page' === $object ) {
     229                                $order_by = 'menu_order, post_title ASC';
     230                        } elseif ( 'date' === $mode || 'page' === $mode ) {
     231                                $order_by = 'post_date DESC';
     232                        } else {
     233                                $order_by = 'menu_order ASC';
     234                        }
     235
     236                        $results = $wpdb->get_results(
     237                                $wpdb->prepare(
     238                                        'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type = %s AND post_status IN (' . self::STATUSES . ') ORDER BY ' . $order_by, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- fixed whitelist above.
     239                                        $object
     240                                )
     241                        );
     242                        foreach ( (array) $results as $key => $row ) {
     243                                $wpdb->update( $wpdb->posts, array( 'menu_order' => $key + 1 ), array( 'ID' => (int) $row->ID ), array( '%d' ), array( '%d' ) );
     244                                clean_post_cache( (int) $row->ID );
     245                        }
     246                }
     247        }
     248
     249        /**
     250         * Re-number term_order of the given taxonomies.
     251         *
     252         * @param array  $tags Taxonomies.
     253         * @param string $sort "keep" (current order) or "name".
     254         */
     255        private function renumber_taxonomies( $tags, $sort = 'keep' ) {
     256                global $wpdb;
     257
     258                foreach ( (array) $tags as $taxonomy ) {
     259                        $result = $wpdb->get_results(
     260                                $wpdb->prepare(
     261                                        "SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min FROM {$wpdb->terms} AS terms INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id ) WHERE term_taxonomy.taxonomy = %s",
     262                                        $taxonomy
     263                                )
     264                        );
     265                        if ( empty( $result ) || 0 == $result[0]->cnt || $result[0]->cnt == $result[0]->max ) {
     266                                continue;
     267                        }
     268
     269                        $order_by = ( 'name' === $sort ) ? 'name ASC' : 'term_order ASC';
     270                        $results  = $wpdb->get_results(
     271                                $wpdb->prepare(
     272                                        "SELECT terms.term_id FROM {$wpdb->terms} AS terms INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id ) WHERE term_taxonomy.taxonomy = %s ORDER BY " . $order_by, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- fixed whitelist above.
     273                                        $taxonomy
     274                                )
     275                        );
     276                        foreach ( (array) $results as $key => $row ) {
     277                                $wpdb->update( $wpdb->terms, array( 'term_order' => $key + 1 ), array( 'term_id' => (int) $row->term_id ), array( '%d' ), array( '%d' ) );
     278                        }
     279                }
     280        }
     281
     282        public function refresh() {
     283                if ( ! current_user_can( 'manage_options' ) ) {
     284                        return;
     285                }
     286
     287                $objects = $this->get_wpso_options_objects();
     288                $tags    = $this->get_wpso_options_tags();
     289
     290                $this->renumber_objects( $objects, 'keep' );
     291                $this->renumber_taxonomies( $tags, 'keep' );
     292
     293                foreach ( $tags as $term ) {
     294                        if ( ! taxonomy_exists( $term ) ) {
     295                                continue;
     296                        }
     297                        $term_items = get_terms(
     298                                array(
     299                                        'taxonomy'   => $term,
     300                                        'hide_empty' => false,
     301                                )
     302                        );
     303                        if ( empty( $term_items ) || is_wp_error( $term_items ) ) {
     304                                continue;
     305                        }
     306                        foreach ( $term_items as $item ) {
     307                                if ( ! is_object( $item ) ) {
     308                                        continue;
     309                                }
     310                                $this->refine_terms_relations( $item->term_id );
     311                                $item_child = get_term_children( $item->term_id, $term );
     312                                if ( ! empty( $item_child ) && ! is_wp_error( $item_child ) ) {
     313                                        foreach ( $item_child as $child ) {
     314                                                $this->refine_terms_relations( $child );
    127315                                        }
    128316                                }
    129                         }                       
    130                        
    131                         if ( empty( $objects ) && empty( $tags ) ) return false;
    132                        
    133                         //pree($objects);
    134                         //pree($tags);
    135                        
    136                         // exclude (sorting, addnew page, edit page)
    137                         if (isset( $_GET['orderby'] ) || strstr( $_SERVER['REQUEST_URI'], 'action=edit' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post-new.php' ) ) return false;
    138                        
    139                         //pree($tags);
    140                        
    141                         if (!$active &&  !empty( $objects ) ) {
    142                                 if ( isset( $_GET['post_type'] ) && !isset( $_GET['taxonomy'] ) && in_array( $_GET['post_type'], $objects ) ) { // if page or custom post types
    143                                         $active = true;
     317                        }
     318                }
     319        }
     320
     321        public function refine_terms_relations( $item ) {
     322                global $wpdb;
     323
     324                $item = absint( $item );
     325                if ( ! $item ) {
     326                        return;
     327                }
     328
     329                $stats = $wpdb->get_results(
     330                        $wpdb->prepare(
     331                                "SELECT count(*) as cnt, max(um.meta_value) as max, min(um.meta_value) as min
     332                                FROM {$wpdb->term_relationships} AS r, {$wpdb->users} AS u, {$wpdb->usermeta} AS um
     333                                WHERE r.term_taxonomy_id = %d AND u.ID = r.object_id
     334                                AND (um.user_id = r.object_id AND um.meta_key = CONCAT('user_order_', r.term_taxonomy_id))",
     335                                $item
     336                        )
     337                );
     338
     339                $user_count1 = $wpdb->get_var(
     340                        $wpdb->prepare(
     341                                "SELECT COUNT(*) FROM {$wpdb->users} u, {$wpdb->term_relationships} AS r WHERE r.term_taxonomy_id = %d AND u.ID = r.object_id",
     342                                $item
     343                        )
     344                );
     345
     346                $user_count2 = $wpdb->get_var(
     347                        $wpdb->prepare(
     348                                "SELECT COUNT(*) FROM {$wpdb->usermeta} um WHERE um.meta_key = %s",
     349                                'user_order_' . $item
     350                        )
     351                );
     352
     353                if ( $user_count1 != $user_count2 ) {
     354                        // Counts differ: drop the stored order for this term so it is rebuilt below.
     355                        $dres = $wpdb->get_results(
     356                                $wpdb->prepare(
     357                                        "SELECT um.umeta_id, um.user_id FROM {$wpdb->usermeta} um WHERE um.meta_key = %s",
     358                                        'user_order_' . $item
     359                                )
     360                        );
     361                        foreach ( (array) $dres as $res ) {
     362                                $wpdb->delete( $wpdb->usermeta, array( 'umeta_id' => (int) $res->umeta_id ), array( '%d' ) );
     363                        }
     364                }
     365
     366                if ( ! empty( $stats ) && $stats[0]->cnt == $stats[0]->max ) {
     367                        return;
     368                }
     369
     370                $relations = $wpdb->get_results(
     371                        $wpdb->prepare(
     372                                "SELECT um.user_id, um.meta_value
     373                                FROM {$wpdb->term_relationships} AS r, {$wpdb->users} AS u, {$wpdb->usermeta} AS um
     374                                WHERE r.term_taxonomy_id = %d AND u.ID = r.object_id
     375                                AND (um.user_id = r.object_id AND um.meta_key = CONCAT('user_order_', r.term_taxonomy_id))",
     376                                $item
     377                        )
     378                );
     379
     380                if ( empty( $relations ) ) {
     381                        $relations = $wpdb->get_results(
     382                                $wpdb->prepare(
     383                                        "SELECT u.ID AS user_id FROM {$wpdb->term_relationships} AS r, {$wpdb->users} AS u WHERE r.term_taxonomy_id = %d AND u.ID = r.object_id",
     384                                        $item
     385                                )
     386                        );
     387                }
     388
     389                foreach ( (array) $relations as $k => $res ) {
     390                        update_user_meta( (int) $res->user_id, 'user_order_' . $item, $k + 1 );
     391                }
     392        }
     393
     394        /* ------------------------------------------------------------------ *
     395         * AJAX helpers
     396         * ------------------------------------------------------------------ */
     397
     398        /**
     399         * Common AJAX gate: capability + nonce.
     400         *
     401         * @param bool $admin_only Restrict to administrators regardless of pro role settings.
     402         */
     403        private function ajax_gate( $admin_only = false ) {
     404                $allowed = $admin_only ? current_user_can( 'manage_options' ) : wpso_user_can_sort();
     405                if ( ! $allowed ) {
     406                        wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ), '', array( 'response' => 403 ) );
     407                }
     408
     409                $nonce = isset( $_POST['wpso_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['wpso_nonce'] ) ) : '';
     410                if ( ! $nonce || ! wp_verify_nonce( $nonce, 'wpso_nonce_ensured' ) ) {
     411                        wp_die( esc_html__( 'Sorry! Your nonce did not verify.', 'wpso-sort-order' ), '', array( 'response' => 403 ) );
     412                }
     413        }
     414
     415        /**
     416         * Parse jQuery UI "serialize()" output into a flat, ordered list of integer ids.
     417         *
     418         * @return int[]
     419         */
     420        private function parse_order_ids() {
     421                $raw = isset( $_POST['order'] ) && is_string( $_POST['order'] ) ? wp_unslash( $_POST['order'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- parsed + cast to int below.
     422                if ( '' === $raw || strlen( $raw ) > 50000 ) {
     423                        return array();
     424                }
     425                parse_str( $raw, $data );
     426
     427                $ids = array();
     428                if ( is_array( $data ) ) {
     429                        foreach ( $data as $values ) {
     430                                if ( ! is_array( $values ) ) {
     431                                        continue;
    144432                                }
    145                                 if ( !isset( $_GET['post_type'] ) && strstr( $_SERVER['REQUEST_URI'], 'wp-admin/edit.php' ) && in_array( 'post', $objects ) ) { // if post
    146                                         $active = true;
     433                                foreach ( $values as $id ) {
     434                                        if ( is_scalar( $id ) && absint( $id ) ) {
     435                                                $ids[] = absint( $id );
     436                                        }
    147437                                }
    148438                        }
    149                        
    150                        
    151                        
    152 
    153                        
    154                        
    155                         //pree($active);
    156                        
    157                         if (!$active && !empty( $tags ) ) {
    158                                
    159                                 if ( isset( $_GET['taxonomy'] ) && in_array( $_GET['taxonomy'], $tags ) ) {
    160                                         $active = true;
     439                }
     440                return array_slice( $ids, 0, 2000 );
     441        }
     442
     443        public function update_menu_order() {
     444                global $wpdb;
     445
     446                $this->ajax_gate();
     447                $ids = $this->parse_order_ids();
     448
     449                foreach ( $ids as $id ) {
     450                        if ( ! current_user_can( 'edit_post', $id ) ) {
     451                                wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ), '', array( 'response' => 403 ) );
     452                        }
     453                }
     454
     455                do_action( 'wpso_before_order_update', 'post', $ids );
     456
     457                $counter = 0;
     458                foreach ( $ids as $id ) {
     459                        ++$counter;
     460                        $wpdb->update( $wpdb->posts, array( 'menu_order' => $counter ), array( 'ID' => $id ), array( '%d' ), array( '%d' ) );
     461                        clean_post_cache( $id );
     462                }
     463
     464                do_action( 'wpso_order_updated', 'post', $ids );
     465                wp_die();
     466        }
     467
     468        public function wpso_increment_order( $arr = array(), $order = 0 ) {
     469                while ( in_array( $order, $arr, true ) ) {
     470                        ++$order;
     471                }
     472                return $order;
     473        }
     474
     475        public function update_menu_order_tags() {
     476                global $wpdb;
     477
     478                $this->ajax_gate();
     479                $ids = $this->parse_order_ids();
     480
     481                foreach ( $ids as $id ) {
     482                        if ( ! current_user_can( 'edit_term', $id ) ) {
     483                                wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ), '', array( 'response' => 403 ) );
     484                        }
     485                }
     486
     487                do_action( 'wpso_before_order_update', 'term', $ids );
     488
     489                $menu_order_arr = array();
     490                foreach ( $ids as $id ) {
     491                        $row = $wpdb->get_row( $wpdb->prepare( "SELECT term_order FROM {$wpdb->terms} WHERE term_id = %d", $id ) );
     492                        if ( is_object( $row ) ) {
     493                                $menu_order_arr[] = $this->wpso_increment_order( $menu_order_arr, (int) $row->term_order );
     494                        }
     495                }
     496                sort( $menu_order_arr );
     497
     498                foreach ( $ids as $position => $id ) {
     499                        if ( array_key_exists( $position, $menu_order_arr ) ) {
     500                                $wpdb->update( $wpdb->terms, array( 'term_order' => (int) $menu_order_arr[ $position ] ), array( 'term_id' => $id ), array( '%d' ), array( '%d' ) );
     501                        }
     502                }
     503                clean_term_cache( $ids );
     504
     505                do_action( 'wpso_order_updated', 'term', $ids );
     506                wp_die();
     507        }
     508
     509        public function current_tax( $params ) {
     510                $valid_tax = 0;
     511                if ( ! empty( $params ) && is_array( $params ) ) {
     512                        $tags = $this->get_wpso_options_tags();
     513                        foreach ( $params as $tax => $term ) {
     514                                if ( ! is_string( $tax ) || ! is_string( $term ) || ! in_array( $tax, $tags, true ) ) {
     515                                        continue;
    161516                                }
    162                                
    163                                 foreach($tags as $tag){
    164                                         if(isset($_GET[$tag])){
    165                                                 $active = true;
     517                                $obj = get_term_by( 'slug', sanitize_text_field( wp_unslash( $term ) ), $tax );
     518                                if ( is_object( $obj ) && isset( $obj->term_id ) ) {
     519                                        $valid_tax = (int) $obj->term_id;
     520                                        break;
     521                                }
     522                        }
     523                }
     524                return $valid_tax;
     525        }
     526
     527        public function update_menu_order_users() {
     528                $this->ajax_gate();
     529
     530                $ids     = $this->parse_order_ids();
     531                $referer = isset( $_POST['referer_string'] ) && is_string( $_POST['referer_string'] ) ? wp_unslash( $_POST['referer_string'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- parsed, every value re-sanitized in current_tax().
     532                $params  = array();
     533                if ( strlen( $referer ) < 2000 ) {
     534                        parse_str( $referer, $params );
     535                }
     536
     537                $valid_tax = $this->current_tax( $params );
     538                if ( ! $valid_tax ) {
     539                        wp_die();
     540                }
     541
     542                foreach ( $ids as $id ) {
     543                        if ( ! current_user_can( 'edit_user', $id ) ) {
     544                                wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ), '', array( 'response' => 403 ) );
     545                        }
     546                }
     547
     548                do_action( 'wpso_before_order_update', 'user', $ids );
     549
     550                $user_order     = 'user_order_' . $valid_tax;
     551                $menu_order_arr = array();
     552                foreach ( $ids as $id ) {
     553                        $menu_order_arr[] = get_user_meta( $id, $user_order, true );
     554                }
     555                sort( $menu_order_arr );
     556
     557                foreach ( $ids as $position => $id ) {
     558                        $value = isset( $menu_order_arr[ $position ] ) && is_numeric( $menu_order_arr[ $position ] ) ? (int) $menu_order_arr[ $position ] : '';
     559                        update_user_meta( $id, $user_order, $value );
     560                }
     561
     562                do_action( 'wpso_order_updated', 'user', $ids );
     563                wp_die();
     564        }
     565
     566        public function update_menu_order_extras() {
     567                $this->ajax_gate( true );
     568
     569                $ids = array();
     570                $raw = isset( $_POST['order'] ) && is_string( $_POST['order'] ) ? wp_unslash( $_POST['order'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- every value is filtered below.
     571                if ( '' !== $raw && strlen( $raw ) < 50000 ) {
     572                        parse_str( $raw, $data );
     573                        if ( is_array( $data ) ) {
     574                                foreach ( $data as $values ) {
     575                                        if ( ! is_array( $values ) ) {
    166576                                                continue;
    167577                                        }
    168                                 }
    169                        
    170                                        
    171                                 if ($wpso_pro && strstr( $_SERVER['REQUEST_URI'], 'wp-admin/users.php' ) && in_array( 'usercategories', $tags ) ) { // if post
    172                                         $active = true;
    173                                 }                               
    174                                
    175                                
    176                         }
    177                        
    178                         //pree($active);
    179                        
    180                         return $active;
    181                 }
    182        
    183                 function load_script_css()
    184                 {
    185                         //$all_plugins = get_plugins();
    186                        
    187                         $translation_array = array(
    188                                 'ajaxurl' => admin_url( 'admin-ajax.php' ),     
    189                                 'this_url' => admin_url('admin.php?page=wpso-settings'),
    190                                 'wpso_tab' => (isset($_GET['t'])?esc_attr($_GET['t']):'0'),
    191                                 'nonce'=> wp_create_nonce('wpso_nonce_ensured'),
    192 
    193                         );
    194                        
    195                        
    196                         if ( $this->_check_load_script_css() ) {
    197                                 wp_enqueue_script( 'jquery' );
    198                                 wp_enqueue_script( 'jquery-ui-sortable' );
    199                                 wp_enqueue_script( 'wpsojs', WPSO_URL.'/js/scripts.js', array( 'jquery' ), date('Ymdh'), true );                               
    200                                 wp_enqueue_style( 'wpso', WPSO_URL.'/css/styles.css', array(), date('Ymdh') );
    201                         }
    202                        
    203                         if(is_admin() && isset($_GET['page']) && $_GET['page'] == 'wpso-settings'){
    204 
    205                                 wp_enqueue_style( 'wpso', WPSO_URL.'/css/admin.css', array(), date('Ymdh') );
    206                                 wp_enqueue_script( 'wpsojs', WPSO_URL.'/js/admin.js', array( 'jquery' ), date('Ymdh'), true );
    207 
    208                                
    209                                
    210 
    211                         }               
    212                        
    213                         wp_localize_script('wpsojs', 'wpso_obj', $translation_array);   
    214                 }
    215                
    216                
    217                                
    218                 function refresh()
    219                 {
    220                        
    221                         global $wpdb;
    222                         $objects = $this->get_wpso_options_objects();
    223                         $tags = $this->get_wpso_options_tags();
    224                        
    225                         //pree($objects);
    226                         //pree($tags);
    227                        
    228                         if ( !empty( $objects ) ) {
    229                                 foreach( $objects as $object) {
    230                                         /*$result = $wpdb->get_results( "
    231                                                 SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
    232                                                 FROM $wpdb->posts
    233                                                 WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
    234                                         " );*/
    235                                         $result = $wpdb->get_results( $wpdb->prepare(
    236                                                 "SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
    237                                                 FROM $wpdb->posts
    238                                                 WHERE post_type = %s AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')",
    239                                                 $object
    240                                         ) );
    241                                         if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) continue;
    242                                        
    243                                         $results = $wpdb->get_results( "
    244                                                 SELECT ID
    245                                                 FROM $wpdb->posts
    246                                                 WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
    247                                                 ORDER BY menu_order ASC
    248                                         " );
    249                                         foreach( $results as $key => $result ) {
    250                                                 $wpdb->update( $wpdb->posts, array( 'menu_order' => $key+1 ), array( 'ID' => $result->ID ) );
    251                                         }
    252                                 }
    253                         }
    254        
    255                         $terms = array();
    256                        
    257                         if ( !empty( $tags ) ) {
    258                                 foreach( $tags as $taxonomy ) {
    259                                         /*$result = $wpdb->get_results( "
    260                                                 SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
    261                                                 FROM $wpdb->terms AS terms
    262                                                 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
    263                                                 WHERE term_taxonomy.taxonomy = '".$taxonomy."'
    264                                         " );*/
    265                                         $result = $wpdb->get_results( $wpdb->prepare(
    266                                                 "SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
    267                                                 FROM $wpdb->terms AS terms
    268                                                 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
    269                                                 WHERE term_taxonomy.taxonomy = %s",
    270                                                 $taxonomy
    271                                         ) );
    272                                         //pree($taxonomy);
    273                                         //pree($result);//exit;
    274                                         $terms[] = $taxonomy;
    275                                         $continue = ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max );
    276                                         if ($continue) continue;
    277                                        
    278                                         $results = $wpdb->get_results( "
    279                                                 SELECT terms.term_id
    280                                                 FROM $wpdb->terms AS terms
    281                                                 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
    282                                                 WHERE term_taxonomy.taxonomy = '".$taxonomy."'
    283                                                 ORDER BY term_order ASC
    284                                         " );
    285                                         //pree($results);//exit;
    286                                         foreach( $results as $key => $result ) {
    287                                                 $wpdb->update( $wpdb->terms, array( 'term_order' => $key+1 ), array( 'term_id' => $result->term_id ) );
    288                                         }
    289                                 }
    290                         }
    291                        
    292                         if ( !empty( $terms ) ) {
    293                                 //pree($terms);
    294                                 foreach( $terms as $term ) {
    295                                         $args = array('taxonomy'=>$term,'hide_empty'=>false);
    296                                         $term_items = get_terms($args);
    297                                         //pree($term_items);
    298                                         //continue;
    299                                         if(!empty($term_items)){
    300                                                 foreach( $term_items as $item ) {
    301                                                         if(!is_object($item)){continue;}                                                       
    302                                                         $this->refine_terms_relations($item->term_id);
    303                                                         $item_child = get_term_children( $item->term_id, $term );
    304                                                         if(!empty($item_child)){
    305                                                                 foreach( $item_child as $child ) {
    306                                                                         $this->refine_terms_relations($child);
    307 
    308                                                                 }
    309                                                         }
    310                                                        
    311                                                        
     578                                        foreach ( $values as $position => $id ) {
     579                                                if ( is_scalar( $id ) ) {
     580                                                        $ids[ absint( $position ) ] = preg_replace( '/[^A-Za-z0-9_\-\.]/', '', (string) $id );
    312581                                                }
    313582                                        }
    314583                                }
    315584                        }
    316                        
    317                        
    318                        
    319                 }
    320                
    321 
    322                
    323                 function refine_terms_relations($item){
    324                         global $wpdb;
    325                                                        
    326                                                         //pree($item);
    327                                                         //pree('\n');
    328                                                        
    329                                                         //pree($item);
    330                                                         //pree($item);
    331                                                         //pree($taxonomy);
    332                                                         //$objects = get_objects_in_term($item, $taxonomy);
    333                                                         //pree($objects);
    334                                                        
    335                                                         /*$squery = "
    336                                                                 SELECT count(*) as cnt, max(um.meta_value) as max, min(um.meta_value) as min
    337                                                                 FROM
    338                                                                         $wpdb->term_relationships AS r,
    339                                                                         $wpdb->users AS u,
    340                                                                         $wpdb->usermeta AS um                                                           
    341                                                                 WHERE
    342                                                                         r.term_taxonomy_id=$item
    343                                                                         AND
    344                                                                         u.ID=r.object_id
    345                                                                         AND
    346                                                                         (um.user_id=r.object_id
    347                                                                         AND
    348                                                                         um.meta_key=CONCAT('user_order_', r.term_taxonomy_id))
    349                                                         ";*/
    350                                                        
    351                                                         $squery = $wpdb->prepare(
    352                                                                 "SELECT count(*) as cnt, max(um.meta_value) as max, min(um.meta_value) as min
    353                                                                 FROM
    354                                                                         $wpdb->term_relationships AS r,
    355                                                                         $wpdb->users AS u,
    356                                                                         $wpdb->usermeta AS um                                                           
    357                                                                 WHERE
    358                                                                         r.term_taxonomy_id=%d
    359                                                                         AND
    360                                                                         u.ID=r.object_id
    361                                                                         AND
    362                                                                         (um.user_id=r.object_id
    363                                                                         AND
    364                                                                         um.meta_key=CONCAT('user_order_', r.term_taxonomy_id))",
    365                                                                 $item
    366                                                         );
    367                                                         $stats = $wpdb->get_results( $squery );
    368                                                         //if($item==26)
    369                                                         //pree($squery);
    370                                                        
    371                                                         $stats = $wpdb->get_results( $squery );
    372                                                        
    373                                                         //pree($stats);
    374                                                         $cquery = "SELECT COUNT(*) FROM $wpdb->users u, $wpdb->term_relationships AS r WHERE r.term_taxonomy_id=$item AND u.ID=r.object_id";
    375                                                        
    376                                                         $user_count1 = $wpdb->get_var( $cquery );
    377                                                        
    378                                                         /*$cquery = "SELECT COUNT(*) FROM $wpdb->usermeta um WHERE um.meta_key=CONCAT('user_order_', $item)";                                                   
    379 
    380                                                         $user_count2 = $wpdb->get_var( $cquery );*/
    381                                                        
    382                                                         $cquery = $wpdb->prepare(
    383                                                                 "SELECT COUNT(*) FROM $wpdb->usermeta um WHERE um.meta_key = %s",
    384                                                                 'user_order_' . $item
    385                                                         );
    386                                                         $user_count2 = $wpdb->get_var( $cquery );
    387                                                        
    388                                                         /*if($item==26){
    389                                                                 pree($user_count1);
    390                                                                 pree($user_count2);
    391                                                                 pree($cquery);
    392                                                         }*/
    393                                                         //pree($cquery);
    394                                                        
    395                                                        
    396                                                         //pree($user_count.' > '.$stats[0]->cnt);
    397                                                         //pree('<br />');
    398                                                         if(($user_count1!=$user_count2)){ //|| $user_count1!=$stats[0]->cnt){
    399                                                                 /*$dquery = "
    400                                                                         SELECT
    401                                                                                 um.umeta_id,
    402                                                                                 um.user_id
    403                                                                         FROM
    404                                                                                 $wpdb->usermeta um
    405                                                                         WHERE                                                                           
    406                                                                                 um.meta_key=CONCAT('user_order_', $item)
    407                                                                                
    408                                                                 ";*/
    409                                                                 $dquery = $wpdb->prepare(
    410                                                                         "SELECT um.umeta_id, um.user_id
    411                                                                         FROM $wpdb->usermeta um
    412                                                                         WHERE um.meta_key = %s",
    413                                                                         'user_order_' . $item
    414                                                                 );
    415                                                                 $dres = $wpdb->get_results($dquery);
    416                                                                 //if($item==26)
    417                                                                 //pree($dquery);
    418                                                                
    419                                                                 $dres = $wpdb->get_results($dquery);
    420                                                                 //pree($dres);
    421                                                                 if(!empty($dres)){
    422                                                                         foreach($dres as $res){
    423                                                                                 if($res->user_id!=$res->object_id){
    424                                                                                         $dfquery = "DELETE FROM $wpdb->usermeta WHERE umeta_id=$res->umeta_id";
    425                                                                                         //pree($dfquery);
    426                                                                                         $wpdb->query($dfquery);
    427                                                                                 }else{
    428                                                                                         //pree($res->user_id.' - '.$item.' - '.$res->umeta_id);
    429                                                                                 }
    430                                                                         }
    431                                                                 }
    432                                                         }
    433                                                        
    434                                                         //$stats[0]->cnt == 0 ||
    435                                                         $continu = ( $stats[0]->cnt == $stats[0]->max );
    436                                                         if ($continu) return;
    437                                                        
    438                                                        
    439                                                        
    440                                                         $rquery = "
    441                                                                
    442                                                                 SELECT
    443                                                                         um.user_id,
    444                                                                         um.meta_value
    445                                                                 FROM
    446                                                                         $wpdb->term_relationships AS r,
    447                                                                         $wpdb->users AS u,
    448                                                                         $wpdb->usermeta AS um                                                           
    449                                                                 WHERE
    450                                                                         r.term_taxonomy_id=$item
    451                                                                         AND
    452                                                                         u.ID=r.object_id
    453                                                                         AND
    454                                                                         (um.user_id=r.object_id
    455                                                                         AND
    456                                                                         um.meta_key=CONCAT('user_order_', r.term_taxonomy_id))
    457                                                                        
    458                                                         ";
    459                                                         //pree($rquery);
    460                                                         $relations = $wpdb->get_results( $rquery );
    461                                                        
    462                                                         //pree($relations);
    463                                                        
    464                                                         if(!empty($relations)){
    465                                                                 foreach( $relations as $k => $res ) {
    466                                                                         $user_order = 'user_order_'.$item;
    467                                                                         //pree($k);
    468                                                                         //pree($res);
    469                                                                         //$wpdb->update( $wpdb->users, array( 'user_order' => $k+1 ), array( 'ID' => $res->object_id ) );
    470                                                                         update_user_meta(intval($res->user_id), $user_order, $k+1);
    471                                                                 }
    472                                                         }else{
    473                                                                 $rquery = "
    474                                                                
    475                                                                 SELECT
    476                                                                         u.ID AS user_id
    477                                                                 FROM
    478                                                                         $wpdb->term_relationships AS r,
    479                                                                         $wpdb->users AS u                                                       
    480                                                                 WHERE
    481                                                                         r.term_taxonomy_id=$item
    482                                                                         AND
    483                                                                         u.ID=r.object_id
    484                                                                        
    485                                                         ";
    486                                                                 //pree($rquery);
    487                                                                 $relations = $wpdb->get_results( $rquery );
    488                                                                 //pree($relations);
    489                                                                
    490                                                                 if(!empty($relations)){
    491                                                                         foreach( $relations as $k => $res ) {
    492                                                                                 $user_order = 'user_order_'.$item;
    493                                                                                 //pree($k);
    494                                                                                 //pree($res);
    495                                                                                 //$wpdb->update( $wpdb->users, array( 'user_order' => $k+1 ), array( 'ID' => $res->object_id ) );
    496                                                                                 update_user_meta(intval($res->user_id), $user_order, $k+1);
    497                                                                         }
    498                                                                 }
    499                                                         }
    500                                                        
    501                                                                        
    502                 }
    503                
    504                 function update_menu_order()
    505                 {
    506                         global $wpdb;
    507                                
    508                         if ( ! current_user_can( 'manage_options' ) ) {
    509                                 wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ) );
    510                         }
    511                        
    512                         if (
    513                 ! isset( $_POST['wpso_nonce'] )
    514                 || ! wp_verify_nonce( sanitize_text_field( wp_unslash ($_POST['wpso_nonce'])), 'wpso_nonce_ensured' )
    515             ) {
    516 
    517                 wp_die(esc_html__('Sorry! Your nonce did not verify.', 'wpso-sort-order'));
    518 
    519             }else{
    520 
    521                                 parse_str( $_POST['order'], $data );
    522                                
    523                                 if ( !is_array( $data ) ) return false;
    524                                        
    525                                 // get objects per now page
    526                                 $id_arr = array();
    527                                 foreach( $data as $key => $values ) {
    528                                         foreach( $values as $position => $id ) {
    529                                                 $id_arr[] = $id;
    530                                         }
     585                }
     586
     587                update_option( 'wpso_extras_order', $ids, false );
     588                wp_die();
     589        }
     590
     591        /* ------------------------------------------------------------------ *
     592         * Settings
     593         * ------------------------------------------------------------------ */
     594
     595        private function settings_redirect( $tab ) {
     596                wp_safe_redirect(
     597                        add_query_arg(
     598                                array(
     599                                        'page' => 'wpso-settings',
     600                                        'msg'  => 'update',
     601                                        't'    => absint( $tab ),
     602                                ),
     603                                admin_url( 'admin.php' )
     604                        )
     605                );
     606                exit;
     607        }
     608
     609        public function update_options() {
     610                if ( ! isset( $_POST['wpso_submit'] ) ) {
     611                        return false;
     612                }
     613                if ( ! current_user_can( 'manage_options' ) ) {
     614                        wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ), '', array( 'response' => 403 ) );
     615                }
     616                check_admin_referer( 'nonce_wpso' );
     617
     618                global $wpso_allowed_pages, $premium_tags_list;
     619
     620                $objects = isset( $_POST['objects'] ) ? (array) wp_unslash( $_POST['objects'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- whitelisted below.
     621                $tags    = isset( $_POST['tags'] ) ? (array) wp_unslash( $_POST['tags'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- whitelisted below.
     622                $extras  = isset( $_POST['extras'] ) ? (array) wp_unslash( $_POST['extras'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- whitelisted below.
     623
     624                $objects = array_values( array_filter( array_unique( array_map( 'sanitize_key', $objects ) ), 'post_type_exists' ) );
     625                $tags    = array_values( array_filter( array_unique( array_map( 'sanitize_key', $tags ) ), 'taxonomy_exists' ) );
     626                $extras  = array_values( array_intersect( array_map( 'sanitize_text_field', $extras ), array_keys( (array) $wpso_allowed_pages ) ) );
     627
     628                // Premium taxonomies are saved from the Premium tab - keep them.
     629                foreach ( $this->get_wpso_options_tags() as $old_tag ) {
     630                        if ( in_array( $old_tag, (array) $premium_tags_list, true ) && ! in_array( $old_tag, $tags, true ) ) {
     631                                $tags[] = $old_tag;
     632                        }
     633                }
     634
     635                update_option(
     636                        'wpso_options',
     637                        array(
     638                                'objects' => $objects ? $objects : '',
     639                                'tags'    => $tags ? $tags : '',
     640                                'extras'  => $extras ? $extras : '',
     641                        )
     642                );
     643
     644                $this->renumber_objects( $this->get_wpso_options_objects(), 'page' );
     645                $this->renumber_taxonomies( $this->get_wpso_options_tags(), 'name' );
     646
     647                $tab = isset( $_POST['wpso_tn'] ) ? absint( $_POST['wpso_tn'] ) : 0;
     648                $this->settings_redirect( $tab );
     649        }
     650
     651        public function update_options_premium() {
     652                global $premium_tags_list;
     653
     654                if ( ! isset( $_POST['wpso_premium_submit'] ) ) {
     655                        return false;
     656                }
     657                if ( ! current_user_can( 'manage_options' ) ) {
     658                        wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ), '', array( 'response' => 403 ) );
     659                }
     660                check_admin_referer( 'nonce_wpso' );
     661
     662                $tags_old       = $this->get_wpso_options_tags();
     663                $input_options  = get_option( 'wpso_options', array() );
     664                $input_options  = is_array( $input_options ) ? $input_options : array();
     665                $tags_premium   = isset( $_POST['tags'] ) ? (array) wp_unslash( $_POST['tags'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- whitelisted below.
     666                $tags_premium   = array_values( array_intersect( array_map( 'sanitize_key', $tags_premium ), (array) $premium_tags_list ) );
     667                $tags_premium   = array_filter( $tags_premium, 'taxonomy_exists' );
     668
     669                if ( ! empty( $tags_premium ) ) {
     670                        $new_tags = array_unique( array_merge( $tags_old, $tags_premium ) );
     671                } else {
     672                        $new_tags = array_diff( $tags_old, (array) $premium_tags_list );
     673                }
     674
     675                $input_options['tags'] = array_values( $new_tags );
     676                update_option( 'wpso_options', $input_options );
     677
     678                $this->renumber_taxonomies( $this->get_wpso_options_tags(), 'name' );
     679
     680                $tab = isset( $_POST['wpso_tn'] ) ? absint( $_POST['wpso_tn'] ) : 0;
     681                $this->settings_redirect( $tab );
     682        }
     683
     684        /* ------------------------------------------------------------------ *
     685         * Front-end ordering
     686         * ------------------------------------------------------------------ */
     687
     688        public function wpso_previous_post_where( $where ) {
     689                global $post;
     690                $objects = $this->get_wpso_options_objects();
     691                if ( empty( $objects ) ) {
     692                        return $where;
     693                }
     694                if ( is_object( $post ) && isset( $post->post_type ) && in_array( $post->post_type, $objects, true ) ) {
     695                        $where = str_replace( "p.post_date < '" . $post->post_date . "'", 'p.menu_order > ' . (int) $post->menu_order, $where );
     696                }
     697                return $where;
     698        }
     699
     700        public function wpso_previous_post_sort( $orderby ) {
     701                global $post;
     702                $objects = $this->get_wpso_options_objects();
     703                if ( empty( $objects ) ) {
     704                        return $orderby;
     705                }
     706                if ( is_object( $post ) && isset( $post->post_type ) && in_array( $post->post_type, $objects, true ) ) {
     707                        $orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
     708                }
     709                return $orderby;
     710        }
     711
     712        public function hocpo_next_post_where( $where ) {
     713                global $post;
     714                $objects = $this->get_wpso_options_objects();
     715                if ( empty( $objects ) ) {
     716                        return $where;
     717                }
     718                if ( is_object( $post ) && isset( $post->post_type ) && in_array( $post->post_type, $objects, true ) ) {
     719                        $where = str_replace( "p.post_date > '" . $post->post_date . "'", 'p.menu_order < ' . (int) $post->menu_order, $where );
     720                }
     721                return $where;
     722        }
     723
     724        public function wpso_next_post_sort( $orderby ) {
     725                global $post;
     726                $objects = $this->get_wpso_options_objects();
     727                if ( empty( $objects ) ) {
     728                        return $orderby;
     729                }
     730                if ( is_object( $post ) && isset( $post->post_type ) && in_array( $post->post_type, $objects, true ) ) {
     731                        $orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
     732                }
     733                return $orderby;
     734        }
     735
     736        public function wpso_pre_get_posts( $wp_query ) {
     737                $objects = $this->get_wpso_options_objects();
     738                if ( empty( $objects ) ) {
     739                        return false;
     740                }
     741
     742                if ( is_admin() ) {
     743                        if ( isset( $wp_query->query['post_type'] ) && ! isset( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     744                                if ( is_string( $wp_query->query['post_type'] ) && in_array( $wp_query->query['post_type'], $objects, true ) ) {
     745                                        $wp_query->set( 'orderby', 'menu_order' );
     746                                        $wp_query->set( 'order', 'ASC' );
    531747                                }
    532                                
    533                                 // get menu_order of objects per now page
    534                                 $menu_order_arr = array();
    535                                 foreach( $id_arr as $key => $id ) {
    536                                         $results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = ".intval( $id ) );
    537                                         foreach( $results as $result ) {
    538                                                 $menu_order_arr[] = $result->menu_order;
    539                                         }
    540                                 }
    541                                
    542                                 // maintains key association = no
    543                                 sort( $menu_order_arr );
    544                                 $counter = 0;
    545                                 foreach( $data as $key => $values ) {
    546                                         foreach( $values as $position => $id ) {
    547                                                 $counter++;
    548                                                 $wpdb->update( $wpdb->posts, array( 'menu_order' => $counter ), array( 'ID' => intval( $id ) ) );//$menu_order_arr[$position]
    549                                         }
    550                                 }
    551                                
    552                         }
    553                        
    554                         wp_die();
    555                 }
    556                
    557                 function wpso_increment_order($arr=array(), $order=0){
    558                        
    559                         if(in_array($order, $arr)){
    560                                 $order++;
    561                                 $order = $this->wpso_increment_order($arr, $order);
    562                         }
    563                        
    564                         return $order;
    565                 }
    566                
    567                 function update_menu_order_tags()
    568                 {
    569                         global $wpdb;
    570                        
    571                         if ( ! current_user_can( 'manage_options' ) ) {
    572                                 wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ) );
    573                         }
    574                        
    575                         if (
    576                 ! isset( $_POST['wpso_nonce'] )
    577                 || ! wp_verify_nonce( sanitize_text_field( wp_unslash ($_POST['wpso_nonce'])), 'wpso_nonce_ensured' )
    578             ) {
    579 
    580                 wp_die(esc_html__('Sorry! Your nonce did not verify.', 'wpso-sort-order'));
    581 
    582             }else{
    583                                
    584                                
    585                        
    586                                 parse_str( $_POST['order'], $data );
    587                                
    588                                 if ( !is_array( $data ) ) return false;
    589                                
    590                                 $id_arr = array();
    591                                 foreach( $data as $key => $values ) {
    592                                         foreach( $values as $position => $id ) {
    593                                                 $id_arr[] = $id;
    594                                         }
    595                                 }
    596                                
    597        
    598                                 $menu_order_arr = array();
    599                                 foreach( $id_arr as $key => $id ) {
    600                                         $row = $wpdb->get_row( "SELECT term_order FROM $wpdb->terms WHERE term_id = ".intval( $id ) );
    601                                        
    602                                         if(is_object($row) && !empty($row)){
    603                                                 $menu_order_arr[] = $this->wpso_increment_order($menu_order_arr, $row->term_order);
    604                                         }
    605                                 }
    606                                 sort( $menu_order_arr );
    607        
    608                                
    609                                 foreach( $data as $key => $values ) {
    610                                         foreach( $values as $position => $id ) {                                       
    611                                                 if(array_key_exists($position, $menu_order_arr)){
    612                                                         $term_order = $menu_order_arr[$position];                                               
    613                                                         $wpdb->update( $wpdb->terms, array( 'term_order' => $term_order ), array( 'term_id' => intval( $id ) ) );
    614        
    615                                                 }
    616                                         }
    617                                 }
    618                        
    619                         }
    620                        
    621                         wp_die();
    622                 }
    623                
    624                 function current_tax($params){
    625    
    626                         $valid_tax = 0;
    627                         if(!empty($params)){
    628                                 $tags = $this->get_wpso_options_tags();
    629                                 foreach($params as $tax=>$term){
    630                                         if(in_array($tax, $tags)){
    631                                                 $obj = get_term_by( 'slug', $term, $tax );
    632                                                 if ( is_object( $obj ) && isset( $obj->term_id ) ) {
    633                                                         $valid_tax = $obj->term_id;
    634                                                         break;
    635                                                 }
    636                                         }
    637                                 }
    638                         }   
    639                         return $valid_tax;
    640                 }
    641        
    642                 function update_menu_order_users()
    643                 {
    644                         global $wpdb;
    645                        
    646                         if ( ! current_user_can( 'manage_options' ) ) {
    647                                 wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ) );
    648                         }
    649                        
    650                         if (
    651                 ! isset( $_POST['wpso_nonce'] )
    652                 || ! wp_verify_nonce( sanitize_text_field( wp_unslash ($_POST['wpso_nonce'])), 'wpso_nonce_ensured' )
    653             ) {
    654 
    655                 wp_die(esc_html__('Sorry! Your nonce did not verify.', 'wpso-sort-order'));
    656 
    657             }else{
    658                        
    659                                 parse_str( $_POST['order'], $data );
    660                                 parse_str( $_POST['referer_string'], $params);
    661                                 //pree($data);
    662                                 //pree($params);
    663                                 $valid_tax = $this->current_tax($params);
    664                                 //pree($valid_tax);
    665                                
    666                                 if ( !is_array( $data ) ) return false;
    667                                
    668                                 $id_arr = array();
    669                                 foreach( $data as $key => $values ) {
    670                                         foreach( $values as $position => $id ) {
    671                                                 $id_arr[] = $id;
    672                                         }
    673                                 }
    674                                 //pree($id_arr);
    675                                 $user_order = 'user_order_'.$valid_tax;
    676                                 //pree($user_order);
    677                                 $menu_order_arr = array();
    678                                 foreach( $id_arr as $key => $id ) {
    679                                         //$results = $wpdb->get_results( "SELECT user_order FROM $wpdb->users WHERE ID = ".intval( $id ) );
    680                                         //foreach( $results as $result ) {
    681                                                 $menu_order_arr[] = get_user_meta( $id, $user_order, true ); //$result->user_order;
    682                                         //}
    683                                 }
    684                                 sort( $menu_order_arr );
    685                                
    686                                 //pree($data);
    687                                 //pree($menu_order_arr);
    688                                
    689                                 foreach( $data as $key => $values ) {
    690                                         foreach( $values as $position => $id ) {
    691                                                 //pree($position.' - '.$id.' - '.$menu_order_arr[$position]);
    692                                                 //pree($menu_order_arr[$position]);
    693                                                 //$wpdb->update( $wpdb->users, array( 'user_order' => $menu_order_arr[$position] ), array( 'ID' => intval( $id ) ) );
    694                                                 //pree(intval($id).' - '.$user_order.' - '.$menu_order_arr[$position]);
    695                                                 //pree(($menu_order_arr[$position]));
    696                                                 update_user_meta(intval($id), $user_order, sanitize_wpso_data($menu_order_arr[$position]));
    697                                         }
    698                                 }
    699                                
    700                         }
    701                         wp_die();
    702                        
    703                 }       
    704                
    705                 function update_menu_order_extras()
    706                 {
    707                         global $wpdb;
    708                        
    709                         if ( ! current_user_can( 'manage_options' ) ) {
    710                                 wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ) );
    711                         }
    712                        
    713                         if (
    714                 ! isset( $_POST['wpso_nonce'] )
    715                 || ! wp_verify_nonce( sanitize_text_field( wp_unslash ($_POST['wpso_nonce'])), 'wpso_nonce_ensured' )
    716             ) {
    717 
    718                 wp_die(esc_html__('Sorry! Your nonce did not verify.', 'wpso-sort-order'));
    719 
    720             }else{
    721                        
    722                                 parse_str( $_POST['order'], $data );
    723                                
    724                                 if ( !is_array( $data ) ) return false;
    725                                
    726                                 $id_arr = array();
    727                                 foreach( $data as $key => $values ) {
    728                                        
    729                                         foreach( $values as $position => $id ) {
    730                                                 $id_arr[$position] = $id;
    731                                         }
    732                                        
    733                                 }
    734        
    735                                 update_option('wpso_extras_order', sanitize_wpso_data($id_arr));
    736                                
    737                         }
    738                        
    739                         wp_die();
    740                        
    741                 }                       
    742                 function update_options()
    743                 {
    744                         global $wpdb;
    745                        
    746                         if ( !isset( $_POST['wpso_submit'] ) ) return false;
    747                                
    748                         check_admin_referer( 'nonce_wpso' );
    749                                
    750             $input_options = array();
    751                         $input_options['objects'] = isset( $_POST['objects'] ) ? sanitize_wpso_data($_POST['objects']) : '';
    752                         $input_options['tags'] = isset( $_POST['tags'] ) ? sanitize_wpso_data($_POST['tags']) : '';
    753                         $input_options['extras'] = isset( $_POST['extras'] ) ? sanitize_wpso_data($_POST['extras']) : '';
    754                                                
    755                        
    756                         update_option( 'wpso_options', sanitize_wpso_data($input_options) );
    757                        
    758                         $objects = $this->get_wpso_options_objects();
    759                         $tags = $this->get_wpso_options_tags();
    760                        
    761                         if ( !empty( $objects ) ) {
    762                                 foreach( $objects as $object ) {
    763                                         $result = $wpdb->get_results( "
    764                                                 SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
    765                                                 FROM $wpdb->posts
    766                                                 WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
    767                                         " );
    768                                         if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) continue;
    769                                        
    770                                         if ( $object == 'page' ) {
    771                                                 $results = $wpdb->get_results( "
    772                                                         SELECT ID
    773                                                         FROM $wpdb->posts
    774                                                         WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
    775                                                         ORDER BY menu_order, post_title ASC
    776                                                 " );
    777                                         } else {
    778                                                 $results = $wpdb->get_results( "
    779                                                         SELECT ID
    780                                                         FROM $wpdb->posts
    781                                                         WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
    782                                                         ORDER BY post_date DESC
    783                                                 " );
    784                                         }
    785                                         foreach( $results as $key => $result ) {
    786                                                 $wpdb->update( $wpdb->posts, array( 'menu_order' => $key+1 ), array( 'ID' => $result->ID ) );
    787                                         }
    788                                 }
    789                         }
    790                        
    791                         if ( !empty( $tags ) ) {
    792                                 foreach( $tags as $taxonomy ) {
    793                                         $result = $wpdb->get_results( "
    794                                                 SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
    795                                                 FROM $wpdb->terms AS terms
    796                                                 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
    797                                                 WHERE term_taxonomy.taxonomy = '".$taxonomy."'
    798                                         " );
    799                                         if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) continue;
    800                                        
    801                                         $results = $wpdb->get_results( "
    802                                                 SELECT terms.term_id
    803                                                 FROM $wpdb->terms AS terms
    804                                                 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
    805                                                 WHERE term_taxonomy.taxonomy = '".$taxonomy."'
    806                                                 ORDER BY name ASC
    807                                         " );
    808                                         foreach( $results as $key => $result ) {
    809                                                 $wpdb->update( $wpdb->terms, array( 'term_order' => $key+1 ), array( 'term_id' => $result->term_id ) );
    810                                         }
    811                                 }
    812                         }
    813                        
    814             $tab = isset($_POST['wpso_tn']) ? sanitize_wpso_data($_POST['wpso_tn']) : 0;
    815             wp_redirect( 'admin.php?page=wpso-settings&msg=update&t='.$tab );
    816                        
    817                 }
    818 
    819        function update_options_premium(){
    820             global $wpdb, $premium_tags_list;
    821 
    822             if ( !isset( $_POST['wpso_premium_submit'] ) ) return false;
    823                        
    824                         check_admin_referer( 'nonce_wpso' );
    825                        
    826             $tags_old = $this->get_wpso_options_tags();
    827             $input_options = get_option('wpso_options', array());
    828             $tags_premium = isset( $_POST['tags'] ) ? sanitize_wpso_data($_POST['tags']) : array();
    829 
    830 
    831             if(!empty($tags_premium)){
    832 
    833                 $new_tags = array_merge($tags_old, $tags_premium);
    834                 $new_tags = array_unique($new_tags);
    835 
    836             }else{
    837 
    838                 $new_tags = array_diff($tags_old, $premium_tags_list);
    839 
    840 
    841             }
    842 
    843 
    844 
    845            
    846 
    847             $input_options['tags'] = $new_tags;
    848 
    849 
    850             update_option( 'wpso_options', sanitize_wpso_data($input_options) );
    851 
    852             $tags = $this->get_wpso_options_tags();
    853 
    854             if ( !empty( $tags ) ) {
    855                 foreach( $tags as $taxonomy ) {
    856                     $result = $wpdb->get_results( "
    857                                                 SELECT count(*) as cnt, max(term_order) as max, min(term_order) as min
    858                                                 FROM $wpdb->terms AS terms
    859                                                 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
    860                                                 WHERE term_taxonomy.taxonomy = '".$taxonomy."'
    861                                         " );
    862                     if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) continue;
    863 
    864                     $results = $wpdb->get_results( "
    865                                                 SELECT terms.term_id
    866                                                 FROM $wpdb->terms AS terms
    867                                                 INNER JOIN $wpdb->term_taxonomy AS term_taxonomy ON ( terms.term_id = term_taxonomy.term_id )
    868                                                 WHERE term_taxonomy.taxonomy = '".$taxonomy."'
    869                                                 ORDER BY name ASC
    870                                         " );
    871                     foreach( $results as $key => $result ) {
    872                         $wpdb->update( $wpdb->terms, array( 'term_order' => $key+1 ), array( 'term_id' => $result->term_id ) );
    873                     }
    874                 }
    875             }
    876 
    877             $tab = isset($_POST['wpso_tn']) ? sanitize_wpso_data($_POST['wpso_tn']) : 0;
    878             wp_redirect( 'admin.php?page=wpso-settings&msg=update&t='.$tab );
    879         }
    880                                
    881                 function wpso_previous_post_where( $where )
    882                 {
    883                         global $post;
    884        
    885                         $objects = $this->get_wpso_options_objects();
    886                         if ( empty( $objects ) ) return $where;
    887                        
    888                         if ( is_object($post) && isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
    889                                 $current_menu_order = $post->menu_order;
    890                                 $where = str_replace( "p.post_date < '".$post->post_date."'", "p.menu_order > '".$current_menu_order."'", $where );
    891                         }
    892                         return $where;
    893                 }
    894                
    895                 function wpso_previous_post_sort( $orderby )
    896                 {
    897                         global $post;
    898                        
    899                         $objects = $this->get_wpso_options_objects();
    900                         if ( empty( $objects ) ) return $orderby;
    901                        
    902                         if ( is_object($post) && isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
    903                                 $orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
    904                         }
     748                        }
     749                        return;
     750                }
     751
     752                /**
     753                 * For Front End
     754                 */
     755                $home_url    = basename( home_url() );
     756                $request_uri = basename( wpso_request_uri() );
     757
     758                if ( $home_url === $request_uri ) {
     759                        return;
     760                }
     761
     762                $active = false;
     763                if ( isset( $wp_query->query['post_type'] ) ) {
     764                        if ( is_string( $wp_query->query['post_type'] ) && in_array( $wp_query->query['post_type'], $objects, true ) ) {
     765                                $active = true;
     766                        }
     767                } elseif ( in_array( 'post', $objects, true ) ) {
     768                        $active = true;
     769                }
     770
     771                if ( ! $active ) {
     772                        return false;
     773                }
     774
     775                if ( isset( $wp_query->query['suppress_filters'] ) ) {
     776                        if ( 'date' === $wp_query->get( 'orderby' ) ) {
     777                                $wp_query->set( 'orderby', 'menu_order' );
     778                        }
     779                        if ( 'DESC' === $wp_query->get( 'order' ) ) {
     780                                $wp_query->set( 'order', 'ASC' );
     781                        }
     782                } else {
     783                        if ( ! $wp_query->get( 'orderby' ) ) {
     784                                $wp_query->set( 'orderby', 'menu_order' );
     785                        }
     786                        if ( ! $wp_query->get( 'order' ) ) {
     787                                $wp_query->set( 'order', 'ASC' );
     788                        }
     789                }
     790        }
     791
     792        public function wpso_get_terms_orderby( $orderby, $args ) {
     793                if ( is_admin() ) {
    905794                        return $orderby;
    906795                }
    907                
    908                 function hocpo_next_post_where( $where )
    909                 {
    910                         global $post;
    911        
    912                         $objects = $this->get_wpso_options_objects();
    913                         if ( empty( $objects ) ) return $where;
    914                        
    915                         if ( is_object($post) && isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
    916                                 $current_menu_order = $post->menu_order;
    917                                 $where = str_replace( "p.post_date > '".$post->post_date."'", "p.menu_order < '".$current_menu_order."'", $where );
    918                         }
    919                         return $where;
    920                 }
    921                
    922                 function wpso_next_post_sort( $orderby )
    923                 {
    924                         global $post;
    925                        
    926                         $objects = $this->get_wpso_options_objects();
    927                         if ( empty( $objects ) ) return $orderby;
    928                        
    929                         if ( is_object($post) && isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
    930                                 $orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
    931                         }
     796                $tags = $this->get_wpso_options_tags();
     797                if ( ! isset( $args['taxonomy'] ) || ! is_string( $args['taxonomy'] ) ) {
    932798                        return $orderby;
    933799                }
    934                
    935                 function wpso_pre_get_posts( $wp_query )
    936                 {
    937                        
    938                        
    939                         $objects = $this->get_wpso_options_objects();
    940                         if ( empty( $objects ) ) return false;
    941                        
    942 
    943                         if ( is_admin() ) {
    944                                
    945                                
    946                                 if ( isset( $wp_query->query['post_type'] ) && !isset( $_GET['orderby'] ) ) {
    947                                         if ( in_array( $wp_query->query['post_type'], $objects ) ) {
    948                                                 $wp_query->set( 'orderby', 'menu_order' );
    949                                                 $wp_query->set( 'order', 'ASC' );
    950                                         }
    951                                 }
    952                        
    953                         /**
    954                         * for Front End
    955                         */
    956                        
    957                         } else{ //if(!is_home() && !is_front_page() && !is_feed()) {
    958                                
    959                                 $home_url = (basename(home_url()));
    960                                 $request_uri = (basename($_SERVER['REQUEST_URI']));
    961                                 //pree($home_url.' != '.$request_uri);
    962                                 if($home_url!=$request_uri){
    963                                
    964                                        
    965                                         $debug_backtrace = debug_backtrace();
    966                                                
    967                                         /*$function = is_feed().' * '.$debug_backtrace[0]['function'];
    968                                         $function .= ' / '.$debug_backtrace[1]['function'];
    969                                         $function .= ' / '.$debug_backtrace[2]['function'];
    970                                         $function .= ' / '.$debug_backtrace[3]['function'];
    971                                         $function .= ' / '.$debug_backtrace[4]['function'];
    972                                         $function .= ' / '.$debug_backtrace[5]['function'];
    973                                         $function .= ' / '.$debug_backtrace[6]['function'].'<br /><br />';
    974                                                                        
    975                                         $f = fopen('/alpha.html', 'a+');
    976                                         fwrite($f, $function);
    977                                         foreach($_SERVER as $k=>$v){
    978                                                 fwrite($f, $k.' = '.$v.'<br />');
    979                                         }
    980                                         fclose($f);*/
    981                                        
    982                                        
    983                                         $active = false;
    984                                        
    985                                         // page or custom post types
    986                                         if ( isset( $wp_query->query['post_type'] ) ) {
    987                                                 // exclude array()
    988                                                 if ( !is_array( $wp_query->query['post_type'] ) ) {
    989                                                         if ( in_array( $wp_query->query['post_type'], $objects ) ) {
    990                                                                 $active = true;
    991                                                         }
    992                                                 }
    993                                         // post
    994                                         } else {
    995                                                 if ( in_array( 'post', $objects ) ) {
    996                                                         $active = true;
    997                                                 }
    998                                         }
    999                                        
    1000                                         if ( !$active ) return false;
    1001                                        
    1002                                         // get_posts()
    1003                                         if ( isset( $wp_query->query['suppress_filters'] ) ) {
    1004                                                 if ( $wp_query->get( 'orderby' ) == 'date' )  $wp_query->set( 'orderby', 'menu_order' );
    1005                                                 if ( $wp_query->get( 'order' ) == 'DESC' ) $wp_query->set( 'order', 'ASC' );
    1006                                         // WP_Query( contain main_query )
    1007                                         } else {
    1008                                                 if ( !$wp_query->get( 'orderby' ) )  $wp_query->set( 'orderby', 'menu_order' );
    1009                                                 if ( !$wp_query->get( 'order' ) ) $wp_query->set( 'order', 'ASC' );
    1010                                         }
    1011                                        
    1012                                 }
    1013                         }
    1014                 }
    1015                
    1016                 function wpso_get_terms_orderby( $orderby, $args )
    1017                 {
    1018                         if ( is_admin() ) return $orderby;
    1019                        
    1020                         $tags = $this->get_wpso_options_tags();
    1021                        
    1022                         if( !isset( $args['taxonomy'] ) ) return $orderby;
    1023                        
    1024                         $taxonomy = $args['taxonomy'];
    1025                         if ( !in_array( $taxonomy, $tags ) ) return $orderby;
    1026                        
    1027                         $orderby = 't.term_order';
     800                if ( ! in_array( $args['taxonomy'], $tags, true ) ) {
    1028801                        return $orderby;
    1029802                }
    1030                
    1031        
    1032                 function wpso_get_object_terms( $terms )
    1033                 {
    1034                         $tags = $this->get_wpso_options_tags();
    1035                        
    1036                         if ( is_admin() && isset( $_GET['orderby'] ) ) return $terms;
    1037                        
    1038                         foreach( $terms as $key => $term ) {
    1039                                 if ( is_object( $term ) && isset( $term->taxonomy ) ) {
    1040                                         $taxonomy = $term->taxonomy;
    1041                                         if ( !in_array( $taxonomy, $tags ) ) return $terms;
    1042                                 } else {
    1043                                         return $terms;
    1044                                 }
    1045                         }
    1046                        
    1047                         usort( $terms, array( $this, 'taxcmp' ) );
     803                return 't.term_order';
     804        }
     805
     806        public function wpso_get_object_terms( $terms ) {
     807                if ( ! is_array( $terms ) ) {
    1048808                        return $terms;
    1049809                }
    1050                
    1051                 function taxcmp( $a, $b )
    1052                 {
    1053                         if ( $a->term_order ==  $b->term_order ) return 0;
    1054                         return ( $a->term_order < $b->term_order ) ? -1 : 1;
    1055                 }
    1056                
    1057                
    1058                 function get_wpso_options_objects()
    1059                 {
    1060                         $wpso_options = get_option( 'wpso_options' ) ? get_option( 'wpso_options' ) : array();
    1061                         //pree($wpso_options);
    1062                         $objects = isset( $wpso_options['objects'] ) && is_array( $wpso_options['objects'] ) ? $wpso_options['objects'] : array();
    1063                         //pree($objects);
    1064                         return $objects;
    1065                 }
    1066                 function get_wpso_options_tags()
    1067                 {
    1068                         $wpso_options = get_option( 'wpso_options' ) ? get_option( 'wpso_options' ) : array();
    1069                         $tags = isset( $wpso_options['tags'] ) && is_array( $wpso_options['tags'] ) ? $wpso_options['tags'] : array();
    1070                         return $tags;
    1071                 }
    1072                 function get_wpso_options_extras()
    1073                 {
    1074                         $wpso_options = get_option( 'wpso_options' ) ? get_option( 'wpso_options' ) : array();
    1075                         $extras = isset( $wpso_options['extras'] ) && is_array( $wpso_options['extras'] ) ? $wpso_options['extras'] : array();
    1076                         return $extras;
    1077                 }               
    1078                
    1079         }
    1080        
    1081         add_action('save_post', 'wpso_updating_new_post_menu_order', 10, 3);
    1082        
    1083         function wpso_updating_new_post_menu_order( $post_id, $post, $update ) {       
    1084                
    1085                
    1086                
    1087                 if ( wp_is_post_revision( $post_id ) || $update)
     810                $tags = $this->get_wpso_options_tags();
     811                if ( is_admin() && isset( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     812                        return $terms;
     813                }
     814                foreach ( $terms as $term ) {
     815                        if ( ! is_object( $term ) || ! isset( $term->taxonomy ) || ! in_array( $term->taxonomy, $tags, true ) ) {
     816                                return $terms;
     817                        }
     818                }
     819                usort( $terms, array( $this, 'taxcmp' ) );
     820                return $terms;
     821        }
     822
     823        public function taxcmp( $a, $b ) {
     824                $a_order = isset( $a->term_order ) ? (int) $a->term_order : 0;
     825                $b_order = isset( $b->term_order ) ? (int) $b->term_order : 0;
     826                if ( $a_order === $b_order ) {
     827                        return 0;
     828                }
     829                return ( $a_order < $b_order ) ? -1 : 1;
     830        }
     831
     832        /* ------------------------------------------------------------------ *
     833         * Options
     834         * ------------------------------------------------------------------ */
     835
     836        private function get_option_list( $key ) {
     837                $wpso_options = get_option( 'wpso_options' );
     838                $wpso_options = is_array( $wpso_options ) ? $wpso_options : array();
     839                return ( isset( $wpso_options[ $key ] ) && is_array( $wpso_options[ $key ] ) ) ? array_values( array_filter( $wpso_options[ $key ], 'is_string' ) ) : array();
     840        }
     841
     842        public function get_wpso_options_objects() {
     843                return $this->get_option_list( 'objects' );
     844        }
     845
     846        public function get_wpso_options_tags() {
     847                return $this->get_option_list( 'tags' );
     848        }
     849
     850        public function get_wpso_options_extras() {
     851                return $this->get_option_list( 'extras' );
     852        }
     853}
     854
     855global $wpso;
     856$wpso = new Wpso();
     857
     858/**
     859 * Newly created posts go to the top.
     860 */
     861function wpso_updating_new_post_menu_order( $post_id, $post, $update ) {
     862        global $wpdb;
     863
     864        if ( wp_is_post_revision( $post_id ) || $update ) {
    1088865                return;
    1089                
    1090                 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    1091         return;
    1092 
    1093                 // Prevent quick edit from clearing custom fields
    1094                 if (defined('DOING_AJAX') && DOING_AJAX)
     866        }
     867        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    1095868                return;
    1096                
    1097                 if ( ! current_user_can( 'edit_post', $post_id ) ) {
    1098                         return;
    1099                 }
    1100                
    1101                 $wpso_options = get_option( 'wpso_options' );
    1102                 $wpso_objects = isset( $wpso_options['objects'] ) ? $wpso_options['objects'] : array();
    1103                 $wpso_objects = is_array($wpso_objects)?$wpso_objects:array();
    1104                
    1105                 global $wpdb;
    1106                
    1107                 if(is_object($post) && !$post->menu_order && in_array( $post->post_type, $wpso_objects )){
    1108                        
    1109                         /*$sql = 'SELECT MAX(menu_order) FROM '.$wpdb->posts.'
    1110                                         WHERE post_type = "'.$post->post_type.'" ';
    1111                         //$max = $wpdb->get_var($sql);
    1112                        
    1113                         $sql = 'SELECT MIN(menu_order) FROM '.$wpdb->posts.'
    1114                                         WHERE post_type = "'.$post->post_type.'" ';             
    1115                         $min = (int)$wpdb->get_var($sql); //30/10/2024*/
    1116                        
    1117                         $sql = $wpdb->prepare(
    1118                                 'SELECT MIN(menu_order) FROM '.$wpdb->posts.'
    1119                                 WHERE post_type = %s',
    1120                                 $post->post_type
    1121                         );
    1122                         $min = (int)$wpdb->get_var($sql);
    1123                        
    1124                         /*if($max>0){
    1125                                 wp_update_post(array(
    1126                                         'ID'=>$post->ID,
    1127                                         'menu_order'=>$max+1
    1128                                 ));
    1129                                
    1130                         }*/
    1131                        
    1132                        
    1133                         wp_update_post(array(
    1134                                 'ID'=>$post->ID,
    1135                                 'menu_order'=>$min-1
    1136                         )); //30/10/2024
    1137                        
    1138                 }
    1139        
    1140         }
    1141          
    1142         add_action('admin_footer', 'wpso_admin_scripts', 100);
    1143         function wpso_admin_scripts(){
    1144                 global $wpso_allowed_pages;
    1145                 $wpso = new Wpso();
    1146 
    1147                 $wpso_extras_order = array();
    1148 ?>
    1149         <script type="text/javascript" language="javascript">
     869        }
     870        if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
     871                return;
     872        }
     873        if ( ! current_user_can( 'edit_post', $post_id ) ) {
     874                return;
     875        }
     876
     877        $wpso_options = get_option( 'wpso_options' );
     878        $wpso_objects = ( is_array( $wpso_options ) && isset( $wpso_options['objects'] ) && is_array( $wpso_options['objects'] ) ) ? $wpso_options['objects'] : array();
     879
     880        if ( is_object( $post ) && ! $post->menu_order && in_array( $post->post_type, $wpso_objects, true ) ) {
     881                $min = (int) $wpdb->get_var(
     882                        $wpdb->prepare( "SELECT MIN(menu_order) FROM {$wpdb->posts} WHERE post_type = %s", $post->post_type )
     883                );
     884                wp_update_post(
     885                        array(
     886                                'ID'         => $post->ID,
     887                                'menu_order' => $min - 1,
     888                        )
     889                );
     890        }
     891}
     892add_action( 'save_post', 'wpso_updating_new_post_menu_order', 10, 3 );
     893
     894/**
     895 * Apply the saved plugin order on the Plugins screen.
     896 */
     897function wpso_admin_scripts() {
     898        global $wpso;
     899
     900        if ( ! is_object( $wpso ) || ! current_user_can( 'manage_options' ) || ! $wpso->_check_load_script_css() ) {
     901                return;
     902        }
     903
     904        $order = get_option( 'wpso_extras_order' );
     905        if ( empty( $order ) || ! is_array( $order ) ) {
     906                return;
     907        }
     908
     909        $map = array();
     910        foreach ( $order as $position => $key ) {
     911                $map[ 'plugin_' . preg_replace( '/[^A-Za-z0-9_\-\.]/', '', (string) $key ) ] = (int) $position;
     912        }
     913        ?>
     914        <script type="text/javascript">
    1150915        jQuery(document).ready(function($){
    1151                 <?php
    1152                 if($wpso->_check_load_script_css()){
    1153                         $wpso_extras_order = get_option('wpso_extras_order');
    1154                         if(!empty($wpso_extras_order)){
    1155 ?>
    1156                                 setTimeout(function(){
    1157 <?php                           
    1158                                 foreach($wpso_extras_order as $order=>$key){
    1159 ?>
    1160                                 $('#plugin_<?php echo $key; ?>').attr('data-order', '<?php echo $order; ?>');
    1161 <?php                                   
    1162                                 }
    1163                                
    1164                                
    1165 ?>
    1166                
    1167                                         var list = $('table.plugins:visible #the-list');
    1168                                        
    1169                                        
    1170                                        
    1171                                         var listItems = list.find('tr').sort(function (a, b) {
    1172                                                                                
    1173                                                 var contentA =parseInt( $(a).attr('data-order'));
    1174                                                 var contentB =parseInt( $(b).attr('data-order'));
    1175                                                 return (contentA < contentB) ? -1 : (contentA > contentB) ? 1 : 0;     
    1176                                                                                        
    1177                                         });
    1178                                         list.find('tr').remove();
    1179                                         list.append(listItems);
    1180                                         return false;
    1181                                
    1182                                 }, 200);
    1183 <?php                           
    1184                         }
    1185                 }
    1186                 ?>
     916                setTimeout(function(){
     917                        var wpsoMap = <?php echo wp_json_encode( $map ); ?>;
     918                        $.each(wpsoMap, function(id, order){
     919                                var el = document.getElementById(id);
     920                                if (el) { $(el).attr('data-order', order); }
     921                        });
     922                        var list = $('table.plugins:visible #the-list');
     923                        var listItems = list.find('tr').sort(function (a, b) {
     924                                var contentA = parseInt($(a).attr('data-order'));
     925                                var contentB = parseInt($(b).attr('data-order'));
     926                                return (contentA < contentB) ? -1 : (contentA > contentB) ? 1 : 0;
     927                        });
     928                        list.find('tr').remove();
     929                        list.append(listItems);
     930                        return false;
     931                }, 200);
    1187932        });
    1188933        </script>
    1189 <?php           
    1190         }       
     934        <?php
     935}
     936add_action( 'admin_footer', 'wpso_admin_scripts', 100 );
  • wp-sort-order/trunk/inc/settings.php

    r3682718 r3776355  
    11<?php if ( ! defined( 'ABSPATH' ) ) exit;
     2if ( ! current_user_can( 'manage_options' ) ) {
     3        wp_die( esc_html__( 'Unauthorized.', 'wpso-sort-order' ), '', array( 'response' => 403 ) );
     4}
    25global $wpdb, $wpso_data, $wpso_pro, $wpso_premium_link, $premium_click, $wpso_allowed_pages, $premium_tags_list;
     6$wpso_pro      = wpso_is_pro();
     7$premium_click = $wpso_pro ? '' : '<small class="premium">(' . esc_html__( 'Premium Feature', 'wpso-sort-order' ) . ')</small>';
    38
    49$wpso_options = get_option( 'wpso_options' );
    5 $wpso_objects = isset( $wpso_options['objects'] ) ? $wpso_options['objects'] : array();
    6 $wpso_tags = isset( $wpso_options['tags'] ) ? $wpso_options['tags'] : array();
    7 $wpso_extras = isset( $wpso_options['extras'] ) ? $wpso_options['extras'] : array();
     10$wpso_options = is_array( $wpso_options ) ? $wpso_options : array();
     11$wpso_objects = ( isset( $wpso_options['objects'] ) && is_array( $wpso_options['objects'] ) ) ? $wpso_options['objects'] : array();
     12$wpso_tags = ( isset( $wpso_options['tags'] ) && is_array( $wpso_options['tags'] ) ) ? $wpso_options['tags'] : array();
     13$wpso_extras = ( isset( $wpso_options['extras'] ) && is_array( $wpso_options['extras'] ) ) ? $wpso_options['extras'] : array();
    814$term_order_exists = $wpdb->query( "DESCRIBE $wpdb->terms `term_order`" );
    915?>
    … …  
    1319<?php //screen_icon( 'plugins' ); ?>
    1420
    15 <h2><?php echo esc_html($wpso_data['Name']).' ('.esc_html($wpso_data['Version']).($wpso_pro?') '.__('Pro', 'wpso-sort-order').'':')'); ?> - <?php echo __('Settings','wpso-sort-order'); ?> <?php echo ($wpso_pro?'':'<a href="'.esc_url($wpso_premium_link).'" target="_blank" class="premium">'.__('Go Premium', 'wpso-sort-order').'</a>'); ?></h2>
     21<h2><?php echo esc_html( $wpso_data['Name'] ) . ' (' . esc_html( $wpso_data['Version'] ) . ( $wpso_pro ? ') ' . esc_html__( 'Pro', 'wpso-sort-order' ) : ')' ); ?> - <?php echo esc_html__( 'Settings', 'wpso-sort-order' ); ?> <?php if ( ! $wpso_pro ) : ?><a href="<?php echo esc_url( $wpso_premium_link ); ?>" target="_blank" rel="noopener noreferrer" class="premium"><?php echo esc_html__( 'Go Premium', 'wpso-sort-order' ); ?></a><?php endif; ?></h2>
    1622
    1723    <h2 class="nav-tab-wrapper">
    1824
    19         <a class="nav-tab nav-tab-active"><?php _e("General", 'wpso-sort-order'); ?> <i class="far fa-check-square"></i></a>
    20         <a class="nav-tab"><?php _e("Premium", 'wpso-sort-order'); ?> <i class="far fa-star"></i></a>
    21         <a class="nav-tab" data-tab="help" data-type="free" style="float:right"><i class="far fa-question-circle"></i>&nbsp;<?php _e("Help", 'wp-sort-order'); ?></a>
     25        <a class="nav-tab nav-tab-active"><?php echo esc_html__( 'General', 'wpso-sort-order' ); ?> <i class="far fa-check-square"></i></a>
     26        <a class="nav-tab"><?php echo esc_html__( 'Premium', 'wpso-sort-order' ); ?> <i class="far fa-star"></i></a>
     27        <a class="nav-tab" data-tab="help" data-type="free" style="float:right"><i class="far fa-question-circle"></i>&nbsp;<?php echo esc_html__( 'Help', 'wpso-sort-order' ); ?></a>
    2228
    2329    </h2>
    2430   
    25 <?php if ( isset($_GET['msg'] )) : ?>
     31<?php if ( 'update' === wpso_get( 'msg' ) ) : ?>
    2632<div id="message" class="updated below-h2">
    27         <?php if ( $_GET['msg'] == 'update' ) : ?>
    28                 <p><?php _e( 'Settings saved.', 'wpso-sort-order'); ?></p>
    29         <?php endif; ?>
     33                <p><?php echo esc_html__( 'Settings saved.', 'wpso-sort-order' ); ?></p>
    3034</div>
    3135<?php endif; ?>
    … …  
    4044</div>
    4145<div id="wpso_select_extras">
    42 <label class="clickable"><input type="checkbox" id="wpso_allcheck_extras" class="hide" /> <?php _e( 'Check/Uncheck', 'wpso-sort-order') ?></label>
     46<label class="clickable"><input type="checkbox" id="wpso_allcheck_extras" class="hide" /> <?php echo esc_html__( 'Check/Uncheck', 'wpso-sort-order' ); ?></label>
    4347<table class="form-table">
    4448        <tbody>
    4549                <tr valign="top">
    46                         <th scope="row"><h4><?php _e('Pages', 'wpso-sort-order') ?></h4><small>menu_order</small></th>
     50                        <th scope="row"><h4><?php echo esc_html__( 'Pages', 'wpso-sort-order' ); ?></h4><small>menu_order</small></th>
    4751                        <td class="options1">
    4852            <ul>
    … …  
    6771</div>
    6872<div id="wpso_select_objects">
    69 <label class="clickable"><input type="checkbox" id="wpso_allcheck_objects" class="hide" /> <?php _e( 'Check/Uncheck', 'wpso-sort-order') ?></label>
     73<label class="clickable"><input type="checkbox" id="wpso_allcheck_objects" class="hide" /> <?php echo esc_html__( 'Check/Uncheck', 'wpso-sort-order' ); ?></label>
    7074<table class="form-table">
    7175        <tbody>
    7276                <tr valign="top">
    73                         <th scope="row"><h4><?php _e( 'Post Types', 'wpso-sort-order') ?></h4><small>menu_order</small></th>
     77                        <th scope="row"><h4><?php echo esc_html__( 'Post Types', 'wpso-sort-order' ); ?></h4><small>menu_order</small></th>
    7478                        <td class="options1">
    7579            <ul>
    … …  
    114118
    115119<div id="wpso_select_tags">
    116 <label class="clickable"><input class="hide" type="checkbox" id="wpso_allcheck_tags"> <?php _e( 'Check/Uncheck', 'wpso-sort-order') ?></label>
     120<label class="clickable"><input class="hide" type="checkbox" id="wpso_allcheck_tags"> <?php echo esc_html__( 'Check/Uncheck', 'wpso-sort-order' ); ?></label>
    117121<table class="form-table">
    118122        <tbody>
    119123                <tr valign="top">
    120                         <th scope="row"><h4><?php _e( 'Taxonomies', 'wpso-sort-order') ?></h4><small>term_order<?php if(!empty($term_order_exists)){ ?> <i title="<?php _e( 'term_order field added to terms database table correctly!', 'wpso-sort-order') ?>" class="fas fa-check-circle"></i><?php } ?></small></th>
     124                        <th scope="row"><h4><?php echo esc_html__( 'Taxonomies', 'wpso-sort-order' ); ?></h4><small>term_order<?php if(!empty($term_order_exists)){ ?> <i title=\"<?php echo esc_attr__( 'term_order field added to terms database table correctly!', 'wpso-sort-order' ); ?>\" class="fas fa-check-circle"></i><?php } ?></small></th>
    121125                        <td class="options2">
    122126            <ul>
    … …  
    146150
    147151                <?php if(!$wpso_pro): ?>
    148                 <li><label><input disabled="disabled" type="checkbox">&nbsp;<?php echo __('User Categories', 'wpso-sort-order'); ?></label>&nbsp;<?php //echo $premium_click; ?></li>
     152                <li><label><input disabled="disabled" type="checkbox">&nbsp;<?php echo esc_html__( 'User Categories', 'wpso-sort-order' ); ?></label>&nbsp;<?php //echo $premium_click; ?></li>
    149153                <?php endif; ?>
    150154            </ul>
    … …  
    154158        <th></th>
    155159        <td><p class="submit">
    156         <input type="submit" class="button-primary" name="wpso_submit" value="<?php _e('Save Changes', 'wpso-sort-order'); ?>">
     160        <input type="submit" class="button-primary" name="wpso_submit" value="<?php echo esc_html__( 'Save Changes', 'wpso-sort-order' ); ?>">
    157161</p></td>
    158162        </tr>
    … …  
    174178
    175179            <?php if ( function_exists( 'wp_nonce_field' ) ) wp_nonce_field( 'nonce_wpso' ); ?>
    176             <input type="hidden" name="wpso_tn" value="<?php echo isset($_GET['t']) ? esc_attr($_GET['t']) : '0'; ?>"/>
     180            <input type="hidden" name="wpso_tn" value="<?php echo esc_attr( absint( wpso_get( 't', '0' ) ) ); ?>"/>
    177181
    178182            <div class="accordion_wrapper">
    179183
    180                 <button class="accordion first"><?php _e('Users List Shortcode','wpso-sort-order'); ?> <span class="dashicons dashicons-plus plus"></span><span class="dashicons dashicons-minus minus"></span></button>
     184                <button class="accordion first"><?php echo esc_html__( 'Users List Shortcode', 'wpso-sort-order' ); ?> <span class="dashicons dashicons-plus plus"></span><span class="dashicons dashicons-minus minus"></span></button>
    181185                <div class="panel first">
    182186                    <div>
    … …  
    186190                                <?php if(!$wpso_pro): ?>
    187191                                    <li>
    188                                         <label><?php echo __('User categories required?', 'wpso-sort-order'); ?></label><?php echo $premium_click; ?>
     192                                        <label><?php echo esc_html__( 'User categories required?', 'wpso-sort-order' ); ?></label><?php echo $premium_click; ?>
    189193                                        <br />
    190194                                        <br />
    … …  
    192196                                <?php endif; ?>
    193197                                <li>
    194                                     <label><?php echo __('A shortcode to list users under taxonomy, terms and children.', 'wpso-sort-order'); ?> <span>[</span>WPSO_USERS <span>slug="</span><?php echo __('taxonomy or term slug', 'wpso-sort-order').'<span>"</span> <span>id="</span>'.__('taxonomy or term id', 'wpso-sort-order'); ?><span>"]</span><?php echo $premium_click; ?></label>
     198                                    <label><?php echo esc_html__( 'A shortcode to list users under taxonomy, terms and children.', 'wpso-sort-order' ); ?> <span>[</span>WPSO_USERS <span>slug="</span><?php echo __('taxonomy or term slug', 'wpso-sort-order').'<span>"</span> <span>id="</span>'.__('taxonomy or term id', 'wpso-sort-order'); ?><span>"]</span><?php echo $premium_click; ?></label>
    195199                                </li>
    196200                            </ul>
    … …  
    205209                </div>
    206210
    207                 <button class="accordion"><?php _e('User Categories','wpso-sort-order'); ?> <span class="dashicons dashicons-plus plus"></span><span class="dashicons dashicons-minus minus"></span></button>
     211                <button class="accordion"><?php echo esc_html__( 'User Categories', 'wpso-sort-order' ); ?> <span class="dashicons dashicons-plus plus"></span><span class="dashicons dashicons-minus minus"></span></button>
    208212                <div class="panel">
    209213
    210214
    211215                    <div id="wpso_select_premium_tags">
    212                         <label class="clickable"><input class="hide" type="checkbox" id="wpso_allcheck_premium_tags"> <?php _e( 'Check/Uncheck', 'wpso-sort-order') ?></label>
     216                        <label class="clickable"><input class="hide" type="checkbox" id="wpso_allcheck_premium_tags"> <?php echo esc_html__( 'Check/Uncheck', 'wpso-sort-order' ); ?></label>
    213217                        <table class="form-table">
    214218                            <tbody>
    215219                            <tr valign="top">
    216                                 <th scope="row"><h4><?php _e( 'Taxonomies', 'wpso-sort-order') ?></h4><small>term_order</small></th>
     220                                <th scope="row"><h4><?php echo esc_html__( 'Taxonomies', 'wpso-sort-order' ); ?></h4><small>term_order</small></th>
    217221                                <td class="options2">
    218222                                    <ul>
    … …  
    243247
    244248                                        <?php if(!$wpso_pro): ?>
    245                                             <li><label><input disabled="disabled" type="checkbox">&nbsp;<?php echo __('User Categories', 'wpso-sort-order'); ?></label>&nbsp;<?php echo $premium_click; ?></li>
     249                                            <li><label><input disabled="disabled" type="checkbox">&nbsp;<?php echo esc_html__( 'User Categories', 'wpso-sort-order' ); ?></label>&nbsp;<?php echo $premium_click; ?></li>
    246250                                        <?php endif; ?>
    247251                                    </ul>
    … …  
    251255                                <th></th>
    252256                                <td><p class="submit">
    253                                         <input type="submit" class="button-primary" name="wpso_premium_submit" value="<?php _e('Save Changes', 'wpso-sort-order'); ?>">
     257                                        <input type="submit" class="button-primary" name="wpso_premium_submit" value="<?php echo esc_html__( 'Save Changes', 'wpso-sort-order' ); ?>">
    254258                                    </p></td>
    255259                            </tr>
    … …  
    276280
    277281        </form>
     282        <?php
     283        if ( $wpso_pro ) {
     284            /**
     285             * Pro add-on renders its own accordion panels here (button.accordion + div.panel).
     286             */
     287            do_action( 'wpso_premium_panels' );
     288        } else {
     289            ?>
     290            <div class="accordion_wrapper wpso-premium-teaser">
     291                <button class="accordion" type="button"><?php echo esc_html__( 'Premium features', 'wpso-sort-order' ); ?> <span class="dashicons dashicons-plus plus"></span><span class="dashicons dashicons-minus minus"></span></button>
     292                <div class="panel">
     293                    <ul style="list-style:disc;margin:18px 0 18px 24px;">
     294                        <li><?php echo esc_html__( 'Order history with one-click undo', 'wpso-sort-order' ); ?></li>
     295                        <li><?php echo esc_html__( 'Backup, export and import of every sort order (JSON)', 'wpso-sort-order' ); ?></li>
     296                        <li><?php echo esc_html__( 'Quick reset: alphabetical, newest, oldest or reversed', 'wpso-sort-order' ); ?></li>
     297                        <li><?php echo esc_html__( 'Let editors and other roles drag & drop what they can edit', 'wpso-sort-order' ); ?></li>
     298                        <li><?php echo esc_html__( 'Order column in post and term list tables', 'wpso-sort-order' ); ?></li>
     299                        <li><?php echo esc_html__( 'Shortcodes to list posts and terms in your sorted order', 'wpso-sort-order' ); ?></li>
     300                        <li><?php echo esc_html__( 'REST API endpoints for headless sites', 'wpso-sort-order' ); ?></li>
     301                        <li><?php echo esc_html__( 'User Categories and the WPSO_USERS shortcode', 'wpso-sort-order' ); ?></li>
     302                    </ul>
     303                    <p><a class="button button-primary" href="<?php echo esc_url( $wpso_premium_link ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html__( 'Go Premium', 'wpso-sort-order' ); ?></a></p>
     304                    <br />
     305                </div>
     306            </div>
     307            <?php
     308        }
     309        ?>
     310
    278311
    279312    </div>
    … …  
    284317        <div class="row mt-3">
    285318                <ul class="position-relative">
    286                 <li class="mb-4"><a class="btn btn-sm btn-info" href="https://wordpress.org/support/plugin/wp-sort-order/" target="_blank"><?php _e('Open a Ticket on Support Forums', 'wp-sort-order'); ?></a></li>
    287                 <li class="mb-4"><a class="btn btn-sm btn-warning mr-2" href="http://demo.androidbubble.com/contact/" target="_blank"><?php _e('Contact Developer', 'wp-sort-order'); ?></a><i class="fas fa-headset"></i></li>
    288                 <li><iframe width="560" height="315" src="https://www.youtube.com/embed/4ZiHUSBDJwY" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
     319                <li class="mb-4"><a class="btn btn-sm btn-info" href="https://wordpress.org/support/plugin/wp-sort-order/" target="_blank" rel="noopener noreferrer"><?php echo esc_html__( 'Open a Ticket on Support Forums', 'wpso-sort-order' ); ?></a></li>
     320                <li class="mb-4"><a class="btn btn-sm btn-warning mr-2" href="https://demo.androidbubble.com/contact/" target="_blank" rel="noopener noreferrer"><?php echo esc_html__( 'Contact Developer', 'wpso-sort-order' ); ?></a><i class="fas fa-headset"></i></li>
     321                <li><iframe width="560" height="315" loading="lazy" referrerpolicy="strict-origin-when-cross-origin" title="WP Sort Order" src="https://www.youtube-nocookie.com/embed/4ZiHUSBDJwY" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
    289322                        </ul>               
    290323                </div>
    … …  
    299332
    300333
    301 <?php if (isset($_GET['t']) || isset($_POST['wpso_tn'])):
    302 
    303 
    304         $wpso_tn = isset($_POST['wpso_tn']) ? intval($_POST['wpso_tn']) : intval($_GET['t']);
     334<?php if ( '' !== wpso_get( 't' ) ) :
     335
     336
     337        $wpso_tn = absint( wpso_get( 't', '0' ) );
    305338
    306339        ?>
  • wp-sort-order/trunk/index.php

    r3682718 r3776355  
    1 <?php if ( ! defined( 'ABSPATH' ) ) exit;
     1<?php
     2/**
     3 * WP Sort Order
     4 *
     5 * @package           WPSortOrder
     6 * @author            Fahad Mahmood
     7 * @license           GPL-2.0-or-later
     8 *
     9 * @wordpress-plugin
     10 * Plugin Name:       WP Sort Order
     11 * Plugin URI:        https://androidbubble.com/blog/wordpress/plugins/wp-sort-order
     12 * Description:       Order plugins, terms (Users, Posts, Pages, Custom Post Types and Custom Taxonomies) using Drag and Drop with jQuery UI Sortable.
     13 * Version:           1.3.7
     14 * Requires at least: 6.0
     15 * Tested up to:      7.1
     16 * Requires PHP:      7.4
     17 * Author:            Fahad Mahmood
     18 * Author URI:        https://www.androidbubble.com
     19 * Text Domain:       wpso-sort-order
     20 * Domain Path:       /languages
     21 * License:           GPL v2 or later
     22 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
     23 */
     24
     25if ( ! defined( 'ABSPATH' ) ) {
     26        exit;
     27}
     28
     29if ( ! defined( 'WPSO_VERSION' ) ) {
     30        define( 'WPSO_VERSION', '1.3.7' );
     31}
     32if ( ! defined( 'WPSO_FILE' ) ) {
     33        define( 'WPSO_FILE', __FILE__ );
     34}
     35if ( ! defined( 'WPSO_URL' ) ) {
     36        define( 'WPSO_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) );
     37}
     38if ( ! defined( 'WPSO_DIR' ) ) {
     39        define( 'WPSO_DIR', plugin_dir_path( __FILE__ ) );
     40}
     41
     42global $wpso_data, $wpso_pro, $wpso_premium_link, $wpso_premium, $premium_click, $wpso_allowed_pages, $premium_tags_list;
     43
     44if ( ! function_exists( 'get_plugin_data' ) ) {
     45        require_once ABSPATH . 'wp-admin/includes/plugin.php';
     46}
     47
     48require_once WPSO_DIR . 'inc/functions.php';
     49
     50$wpso_allowed_pages = array( 'plugins.php' => 'Plugins' );
     51$wpso_data          = get_plugin_data( __FILE__, true, false );
     52$premium_tags_list  = array( 'usercategories' );
     53
    254/*
    3 Plugin Name: WP Sort Order
    4 Plugin URI: http://androidbubble.com/blog/wordpress/plugins/wp-sort-order
    5 Description: Order plugins, terms (Users, Posts, Pages, Custom Post Types and Custom Taxonomies) using a Drag and Drop with jQuery ui Sortable.
    6 Version: 1.3.6
    7 Author: Fahad Mahmood
    8 Author URI: http://www.androidbubble.com
    9 Text Domain: wpso-sort-order
    10 Domain Path: /languages/
    11 License: GPL2
    12 License URI: http://www.gnu.org/licenses/gpl-2.0.html
     55 * PRO "plug and play" switch.
     56 *
     57 * If the /pro directory ships with a loader (pro/wpso_pro.php) or the legacy
     58 * pro/wpso_extended.php, premium features are enabled. If the directory is
     59 * missing, the free version simply keeps working - nothing is ever required
     60 * from /pro by the free code.
     61 */
     62$wpso_premium      = WPSO_DIR . 'pro/wpso_pro.php';
     63$wpso_premium_old  = WPSO_DIR . 'pro/wpso_extended.php';
     64$wpso_pro          = ( is_readable( $wpso_premium ) || is_readable( $wpso_premium_old ) );
     65$wpso_premium_link = 'https://shop.androidbubbles.com/product/wp-sort-order-pro';
    1366
    14 This WordPress Plugin is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or any later version. This free software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this software. If not, see http://www.gnu.org/licenses/gpl-2.0.html.
    15 */
     67// Built lazily in settings.php (translations must not be called before "init").
     68$premium_click = '';
    1669
    17         require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    18         include(plugin_dir_path(__FILE__) . 'inc/functions.php');
     70if ( is_admin() ) {
     71        add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wpso_plugin_links' );
     72}
    1973
    20        
    21         global $wpso_data, $wpso_pro, $wpso_premium_link, $wpso_premium, $premium_click, $wpso_allowed_pages, $premium_tags_list;
    22         $wpso_allowed_pages = array('plugins.php'=>'Plugins');
    23         $wpso_data = get_plugin_data(__FILE__, true, false);
    24         $wpso_dir = plugin_dir_path( __FILE__ );
    25         $premium_tags_list = array('usercategories');
    26         define( 'WPSO_URL', plugins_url( '', __FILE__ ) );
    27         define( 'WPSO_DIR', $wpso_dir );
    28         $wpso_premium = $wpso_dir.'pro/wpso_extended.php';
    29         $wpso_pro = file_exists($wpso_premium);
    30         $wpso_premium_link = 'https://shop.androidbubbles.com/product/wp-sort-order-pro';//https://shop.androidbubble.com/products/wordpress-plugin?variant=36439508713627';//
    31        
    32         $premium_click = $wpso_pro?'':'<small class="premium">('.__('Premium Feature','wpso-sort-order').')</small>';
    33        
    34         if(is_admin()){
    35                 $plugin = plugin_basename(__FILE__);
    36                 add_filter("plugin_action_links_$plugin", 'wpso_plugin_links' );       
    37                
    38         }       
    39        
    40                
    41                
    42         function wpso_activate() {                     
    43                
     74/**
     75 * Load translations.
     76 */
     77function wpso_textdomain() {
     78        load_plugin_textdomain( 'wpso-sort-order', false, dirname( plugin_basename( WPSO_FILE ) ) . '/languages' );
     79}
     80add_action( 'init', 'wpso_textdomain' );
     81
     82function wpso_activate() {
     83        // Schema changes are handled lazily by Wpso::wpso_activation().
     84}
     85register_activation_hook( __FILE__, 'wpso_activate' );
     86
     87require_once WPSO_DIR . 'inc/hooks.php';
     88
     89/*
     90 * Pro loader - last, so it can use every free hook. Wrapped so that a broken or
     91 * partial /pro folder can never take the free plugin (or the site) down.
     92 */
     93if ( $wpso_pro ) {
     94        if ( is_readable( $wpso_premium ) ) {
     95                require_once $wpso_premium;
     96        } elseif ( is_readable( $wpso_premium_old ) ) {
     97                require_once $wpso_premium_old;
    4498        }
    45         register_activation_hook( __FILE__, 'wpso_activate' );
    46        
    47         if($wpso_pro)
    48         include(plugin_dir_path(__FILE__) . 'pro/wpso_extended.php');
    49        
    50         //echo $wpso_premium;
    51        
    52         include(plugin_dir_path(__FILE__) . 'inc/hooks.php');
    53        
     99        /**
     100         * Fires once the pro folder has been loaded.
     101         */
     102        do_action( 'wpso_pro_loaded' );
     103}
  • wp-sort-order/trunk/js/admin.js

    r2416462 r3776355  
    102102                });             
    103103                $('small.premium').on('click', function(){
    104                         window.open($('a.premium').attr('href'));
     104                        window.open($('a.premium').attr('href'), '_blank', 'noopener,noreferrer');
    105105                       
    106106                });
  • wp-sort-order/trunk/js/scripts.js

    r3024631 r3776355  
    11(function($){
    2        
    3         // posts
    42
    5         $('table.posts #the-list, table.pages #the-list').sortable({
    6                 'items': 'tr',
    7                 'axis': 'y',
    8                 'helper': fixHelper,
    9                 'update' : function(e, ui) {
    10                         $.post( wpso_obj.ajaxurl, {
    11                                 action: 'update-menu-order',
    12                                 order: $('#the-list').sortable('serialize'),
    13                                 'wpso_nonce': wpso_obj.nonce,
    14                         });
    15                 }
    16         });
    17         //$("#the-list").disableSelection();
    18        
    19         // tags
    20        
    21         $('table.tags #the-list').sortable({
    22                 'items': 'tr',
    23                 'axis': 'y',
    24                 'helper': fixHelper,
    25                 'update' : function(e, ui) {
    26                         $.post( wpso_obj.ajaxurl, {
    27                                 action: 'update-menu-order-tags',
    28                                 order: $('#the-list').sortable('serialize'),
    29                                 'wpso_nonce': wpso_obj.nonce,
    30                         });
    31                 }
    32         });
    33        
    34         $('table.users #the-list').sortable({
    35                 'items': 'tr',
    36                 'axis': 'y',
    37                 'helper': fixHelper,
    38                 'update' : function(e, ui) {
    39                         var ref = $('input[name="_wp_http_referer"]').val().split('?');
    40                         $.post( wpso_obj.ajaxurl, {
    41                                 action: 'update-menu-order-users',
    42                                 order: $('#the-list').sortable('serialize'),
    43                                 referer_string: ref[1],
    44                                 'wpso_nonce': wpso_obj.nonce,
    45                         });
    46                 }
    47         });     
    48        
    49         $.each($('table.plugins:visible #the-list > tr'), function(i,j){
    50                 var tid = 'plugin_'+$(this).find('input[name="checked[]"]').attr('id').replace('checkbox_', '');
    51                 $(this).attr('id', tid);
    52         });
    53        
    54         $('table.plugins:visible #the-list').sortable({
    55                 'items': 'tr',
    56                 'axis': 'y',
    57                 'helper': fixHelper,
    58                 'refreshPositions': true,
    59                 'update' : function(e, ui) {           
    60                         $.post( wpso_obj.ajaxurl, {
    61                                 action: 'update-menu-order-extras',
    62                                 order: $('#the-list').sortable('serialize'),
    63                                 'wpso_nonce': wpso_obj.nonce,
    64                         });
    65                 }
    66         });             
    67         //$("#the-list").disableSelection();
    68        
     3        if ( typeof wpso_obj === 'undefined' ) {
     4                return;
     5        }
     6
    697        var fixHelper = function(e, ui) {
    708                ui.children().children().each(function() {
    … …  
    7311                return ui;
    7412        };
    75        
    7613
    77        
    78 })(jQuery)
     14        var post_order = function(action, extra) {
     15                var data = $.extend({
     16                        action: action,
     17                        order: $('#the-list').sortable('serialize'),
     18                        'wpso_nonce': wpso_obj.nonce
     19                }, extra || {});
     20                $.post( wpso_obj.ajaxurl, data );
     21        };
     22
     23        // posts
     24        $('table.posts #the-list, table.pages #the-list').sortable({
     25                'items': 'tr',
     26                'axis': 'y',
     27                'helper': fixHelper,
     28                'update' : function(e, ui) {
     29                        post_order('update-menu-order');
     30                }
     31        });
     32
     33        // tags
     34        $('table.tags #the-list').sortable({
     35                'items': 'tr',
     36                'axis': 'y',
     37                'helper': fixHelper,
     38                'update' : function(e, ui) {
     39                        post_order('update-menu-order-tags');
     40                }
     41        });
     42
     43        // users
     44        $('table.users #the-list').sortable({
     45                'items': 'tr',
     46                'axis': 'y',
     47                'helper': fixHelper,
     48                'update' : function(e, ui) {
     49                        var referer = $('input[name="_wp_http_referer"]').val() || '';
     50                        var ref = referer.split('?');
     51                        post_order('update-menu-order-users', { referer_string: ref[1] || '' });
     52                }
     53        });
     54
     55        // plugins
     56        $.each($('table.plugins:visible #the-list > tr'), function(i,j){
     57                var cb = $(this).find('input[name="checked[]"]');
     58                if ( cb.length && cb.attr('id') ) {
     59                        $(this).attr('id', 'plugin_' + cb.attr('id').replace('checkbox_', ''));
     60                }
     61        });
     62
     63        $('table.plugins:visible #the-list').sortable({
     64                'items': 'tr',
     65                'axis': 'y',
     66                'helper': fixHelper,
     67                'refreshPositions': true,
     68                'update' : function(e, ui) {
     69                        post_order('update-menu-order-extras');
     70                }
     71        });
     72
     73})(jQuery);
  • wp-sort-order/trunk/readme.txt

    r3682718 r3776355  
    22Contributors: fahadmahmood, invoicepress
    33Tags: taxonomy order, user order, plugins order, post order
    4 Requires at least: 3.5.0
     4Requires at least: 6.0
    55Tested up to: 7.1
    6 Stable tag: 1.3.6
    7 Requires PHP: 7.0
     6Stable tag: 1.3.7
     7Requires PHP: 7.4
    88License: GPL2
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
    … …  
    1717* Project URI: <http://androidbubble.com/blog/wordpress/plugins/wp-sort-order>
    1818
    19 * License: GPL 3. See License below for copyright jots and titles.
     19* License: GPLv2 or later. See License below.
    2020
    2121
    … …  
    104104
    105105== Changelog ==
     106= 1.3.7 =
     107* Tested up to WordPress 7.1. Requires WordPress 6.0 and PHP 7.4 or newer.
     108* Security: every SQL query is prepared; capability + nonce checks on all settings and AJAX actions; per-item permission checks (edit_post / edit_term / edit_user); whitelisted settings input; safe redirects; escaped output; no nonce exposed on the front end.
     109* Fix: settings saved from the General tab no longer drop Premium taxonomies.
     110* Fix: PHP 8.x notices and a fatal error risk on front-end user queries.
     111* Fix: plugin translations now load (text domain wpso-sort-order); uninstall cleanup moved to uninstall.php.
     112* Assets are versioned with the plugin version instead of changing every minute (better caching).
     113* New: optional "pro" add-on folder. If /pro exists, premium features switch on automatically (history and undo, backup, quick reset, role access, order column, shortcodes, REST API). Without it the free plugin works exactly as before.
    106114= 1.3.6 =
    107115Security: Hardened plugin against SQL injection, XSS, CSRF, LFI, and privilege escalation vulnerabilities with comprehensive input validation, output escaping, and capability checks. [05/09/2026]
    … …  
    155163
    156164== Upgrade Notice ==
     165= 1.3.7 =
     166Security hardening, WordPress 7.1 compatibility and fixes. Requires WordPress 6.0+ and PHP 7.4+.
    157167= 1.3.6 =
    158168Security hardening release: This update fixes multiple security vulnerabilities including SQL injection, XSS, CSRF, file inclusion, and privilege escalation issues. All users are strongly recommended to update immediately.
    … …  
    204214A few improvements related to WordPress 4.5.0.
    205215
     216== External services ==
     217The Help tab of the settings page embeds an optional video from YouTube (youtube-nocookie.com). It loads only when an administrator opens that tab. See https://policies.google.com/privacy.
     218
    206219== License ==
    207220This WordPress Plugin is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or any later version. This free software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this software. If not, see http://www.gnu.org/licenses/gpl-2.0.html.
Note: See TracChangeset for help on using the changeset viewer.