elementor
1 year ago
logs
1 month ago
strong-testimonials-beaver-block
1 year ago
submodules
8 months ago
class-strong-gutemberg.php
1 year ago
class-strong-log.php
1 year ago
class-strong-mail.php
1 year ago
class-strong-testimonials-average-shortcode.php
1 year ago
class-strong-testimonials-count-shortcode.php
1 year ago
class-strong-testimonials-defaults.php
1 month ago
class-strong-testimonials-form.php
1 month ago
class-strong-testimonials-order.php
1 year ago
class-strong-testimonials-privacy.php
1 year ago
class-strong-testimonials-render.php
1 month ago
class-strong-testimonials-templates.php
1 year ago
class-strong-testimonials-view-shortcode.php
1 week ago
class-strong-testimonials-view-widget.php
1 year ago
class-strong-view-display.php
6 days ago
class-strong-view-form.php
6 days ago
class-strong-view-slideshow.php
6 days ago
class-strong-view.php
6 days ago
class-walker-strong-category-checklist-front.php
1 year ago
deprecated.php
1 year ago
filters.php
1 month ago
functions-activation.php
1 month ago
functions-content.php
11 months ago
functions-image.php
5 months ago
functions-rating.php
1 year ago
functions-template-form.php
1 week ago
functions-template.php
4 months ago
functions-views.php
1 year ago
functions.php
1 month ago
l10n-polylang.php
1 year ago
l10n-wpml.php
1 year ago
post-types.php
1 week ago
retro.php
1 year ago
scripts.php
1 month ago
class-strong-testimonials-order.php
305 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Strong_Testimonials_Order |
| 4 | * |
| 5 | * @since 1.16 |
| 6 | */ |
| 7 | class Strong_Testimonials_Order { |
| 8 | |
| 9 | /** |
| 10 | * Strong_Testimonials_Order constructor. |
| 11 | */ |
| 12 | public function __construct() {} |
| 13 | |
| 14 | /** |
| 15 | * Initialize. |
| 16 | */ |
| 17 | public static function init() { |
| 18 | self::add_actions(); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Add actions and filters. |
| 23 | */ |
| 24 | public static function add_actions() { |
| 25 | |
| 26 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_scripts' ) ); |
| 27 | |
| 28 | add_action( 'load-edit.php', array( __CLASS__, 'refresh' ) ); |
| 29 | |
| 30 | add_action( 'wp_ajax_st-update-menu-order', array( __CLASS__, 'update_menu_order' ) ); |
| 31 | |
| 32 | add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ), 500 ); |
| 33 | |
| 34 | add_filter( 'posts_orderby', array( __CLASS__, 'posts_orderby' ), 500, 2 ); |
| 35 | |
| 36 | add_filter( 'get_previous_post_where', array( __CLASS__, 'get_previous_post_where' ) ); |
| 37 | add_filter( 'get_previous_post_sort', array( __CLASS__, 'get_previous_post_sort' ) ); |
| 38 | |
| 39 | add_filter( 'get_next_post_where', array( __CLASS__, 'get_next_post_where' ) ); |
| 40 | add_filter( 'get_next_post_sort', array( __CLASS__, 'get_next_post_sort' ) ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Load admin scripts and styles. |
| 45 | */ |
| 46 | public static function load_scripts() { |
| 47 | |
| 48 | $screen = get_current_screen(); |
| 49 | if ( ! $screen || 'edit-wpm-testimonial' !== $screen->id ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | if ( Strong_Testimonials_Admin_list::is_column_sorted() ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | global $wp_query; |
| 58 | |
| 59 | wp_enqueue_style( 'wpmtst-admin-order-style', WPMTST_ADMIN_URL . '/css/order.css', array(), null ); |
| 60 | wp_enqueue_script( |
| 61 | 'wpmtst-admin-order-script', |
| 62 | WPMTST_ADMIN_URL . 'js/admin-order.js', |
| 63 | array( |
| 64 | 'jquery-effects-highlight', |
| 65 | 'jquery-ui-sortable', |
| 66 | ), |
| 67 | null, |
| 68 | true |
| 69 | ); |
| 70 | |
| 71 | $helper = array( 'nonce' => wp_create_nonce( 'st-update-menu-order' ) ); |
| 72 | if ( isset( $wp_query ) && isset( $wp_query->query_vars ) ) { |
| 73 | $helper['page'] = max( 1, $wp_query->query_vars['paged'] ); |
| 74 | $helper['posts_per_page'] = $wp_query->query_vars['posts_per_page']; |
| 75 | |
| 76 | } |
| 77 | wp_localize_script( 'wpmtst-admin-order-script', 'wpmtstOrderHelper', $helper ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Refresh the post list. |
| 82 | */ |
| 83 | public static function refresh() { |
| 84 | |
| 85 | global $wpdb; |
| 86 | |
| 87 | if ( ! wpmtst_is_testimonial_screen() ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | $result = $wpdb->get_results( "SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min FROM $wpdb->posts WHERE post_type = 'wpm-testimonial' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')" ); |
| 92 | |
| 93 | $count = $result[0]; |
| 94 | |
| 95 | // Exit if already one for one. |
| 96 | if ( 0 === $count->cnt || $count->cnt === $count->max ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Initial or reset |
| 101 | if ( 0 === $count->min && 0 === $count->max ) { |
| 102 | |
| 103 | // Order by descending post date. |
| 104 | $results = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'wpm-testimonial' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future') ORDER BY post_date DESC" ); |
| 105 | |
| 106 | foreach ( $results as $key => $result ) { |
| 107 | $wpdb->update( $wpdb->posts, array( 'menu_order' => $key + 1 ), array( 'ID' => $result->ID ) ); |
| 108 | } |
| 109 | } else { |
| 110 | |
| 111 | // Consecutive reorder with new posts at top. |
| 112 | $results = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'wpm-testimonial' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future') ORDER BY menu_order ASC, post_date DESC" ); |
| 113 | |
| 114 | foreach ( $results as $key => $result ) { |
| 115 | $wpdb->update( $wpdb->posts, array( 'menu_order' => $key + 1 ), array( 'ID' => $result->ID ) ); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Suppress filters on testimonials. |
| 122 | * |
| 123 | * @param $query |
| 124 | */ |
| 125 | public static function pre_get_posts( $query ) { |
| 126 | |
| 127 | if ( is_admin() ) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | if ( ! isset( $query->query['post_type'] ) ) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if ( is_array( $query->query['post_type'] ) ) { |
| 136 | |
| 137 | if ( ! in_array( 'wpm-testimonial', $query->query['post_type'], true ) ) { |
| 138 | return; |
| 139 | } |
| 140 | } else { |
| 141 | |
| 142 | if ( 'wpm-testimonial' !== $query->query['post_type'] ) { |
| 143 | return; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // disable filter suppression |
| 148 | if ( isset( $query->query['suppress_filters'] ) ) { |
| 149 | $query->query['suppress_filters'] = false; |
| 150 | } |
| 151 | |
| 152 | if ( isset( $query->query_vars['suppress_filters'] ) ) { |
| 153 | $query->query_vars['suppress_filters'] = false; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Filter sort parameter. |
| 159 | * |
| 160 | * @param $orderby |
| 161 | * @param $query |
| 162 | * |
| 163 | * @return string |
| 164 | */ |
| 165 | public static function posts_orderby( $orderby, $query ) { |
| 166 | /** |
| 167 | * Default query sort with no parameters is post_date descending. |
| 168 | * |
| 169 | * If given a query sort parameter, e.g. sorting a column in post list |
| 170 | * table, then do nothing. |
| 171 | * |
| 172 | * If no sort parameter given, then add menu_order to default sort. |
| 173 | * (This class is not loaded if reordering is disabled in this plugin |
| 174 | * so no need to check that option before adding menu_order.) |
| 175 | */ |
| 176 | if ( 'wpm-testimonial' === $query->get( 'post_type' ) ) { |
| 177 | if ( ! $query->get( 'orderby' ) ) { |
| 178 | global $wpdb; |
| 179 | $orderby = "{$wpdb->posts}.menu_order ASC, {$wpdb->posts}.post_date DESC"; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return $orderby; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Add menu order to previous post navigation. |
| 188 | * |
| 189 | * @param $where |
| 190 | * |
| 191 | * @return string |
| 192 | */ |
| 193 | public static function get_previous_post_where( $where ) { |
| 194 | global $post; |
| 195 | if ( isset( $post->post_type ) && 'wpm-testimonial' === $post->post_type ) { |
| 196 | $where = "WHERE p.menu_order > '{$post->menu_order}' AND p.post_type = '{$post->post_type}' AND p.post_status = 'publish'"; |
| 197 | } |
| 198 | |
| 199 | return $where; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Add menu order to next post navigation. |
| 204 | * |
| 205 | * @param $where |
| 206 | * |
| 207 | * @return string |
| 208 | */ |
| 209 | public static function get_next_post_where( $where ) { |
| 210 | global $post; |
| 211 | if ( isset( $post->post_type ) && 'wpm-testimonial' === $post->post_type ) { |
| 212 | $where = "WHERE p.menu_order < '{$post->menu_order}' AND p.post_type = '{$post->post_type}' AND p.post_status = 'publish'"; |
| 213 | } |
| 214 | |
| 215 | return $where; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Add menu order to previous post sort. |
| 220 | * |
| 221 | * @param $sort |
| 222 | * |
| 223 | * @return string |
| 224 | */ |
| 225 | public static function get_previous_post_sort( $sort ) { |
| 226 | global $post; |
| 227 | if ( isset( $post->post_type ) && 'wpm-testimonial' === $post->post_type ) { |
| 228 | $sort = 'ORDER BY p.menu_order ASC, p.post_date DESC LIMIT 1'; |
| 229 | } |
| 230 | |
| 231 | return $sort; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Add menu order to next post sort. |
| 236 | * |
| 237 | * @param $sort |
| 238 | * |
| 239 | * @return string |
| 240 | */ |
| 241 | public static function get_next_post_sort( $sort ) { |
| 242 | global $post; |
| 243 | if ( isset( $post->post_type ) && 'wpm-testimonial' === $post->post_type ) { |
| 244 | $sort = 'ORDER BY p.menu_order DESC, p.post_date ASC LIMIT 1'; |
| 245 | } |
| 246 | |
| 247 | return $sort; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Update menu order in back end. |
| 252 | */ |
| 253 | public static function update_menu_order() { |
| 254 | |
| 255 | $nonce = $_POST['nonce']; //phpcs:ignore |
| 256 | |
| 257 | if ( ! wp_verify_nonce( $nonce, 'st-update-menu-order' ) ) { |
| 258 | wp_send_json_error( 'no nonce' ); |
| 259 | die(); |
| 260 | } |
| 261 | |
| 262 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 263 | wp_die(); |
| 264 | } |
| 265 | global $wpdb; |
| 266 | |
| 267 | if ( ! empty( $_POST['posts'] ) ) { |
| 268 | parse_str( sanitize_text_field( wp_unslash( $_POST['posts'] ) ), $data ); |
| 269 | } else { |
| 270 | wp_die(); |
| 271 | } |
| 272 | |
| 273 | if ( ! is_array( $data ) ) { |
| 274 | wp_die(); |
| 275 | } |
| 276 | |
| 277 | $id_arr = $data['post']; |
| 278 | $menu_order_arr = array(); |
| 279 | |
| 280 | // Fetch query vars for pagination. |
| 281 | $paged = 1; |
| 282 | $posts_per_page = 25; |
| 283 | if ( isset( $_POST['order'] ) ) { |
| 284 | if ( isset( $_POST['order']['page'] ) ) { |
| 285 | $paged = absint( $_POST['order']['page'] ); |
| 286 | } |
| 287 | if ( isset( $_POST['order']['posts_per_page'] ) ) { |
| 288 | $posts_per_page = absint( $_POST['order']['posts_per_page'] ); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Reorder |
| 293 | foreach ( $id_arr as $key => $id ) { |
| 294 | $pos = ( $paged - 1 ) * $posts_per_page + ( $key + 1 ); |
| 295 | $wpdb->update( $wpdb->posts, array( 'menu_order' => $pos ), array( 'ID' => intval( $id ) ) ); |
| 296 | $menu_order_arr[] = $pos; |
| 297 | } |
| 298 | |
| 299 | echo json_encode( $menu_order_arr ); |
| 300 | wp_die(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | Strong_Testimonials_Order::init(); |
| 305 |