Advertisement
bedas

Untitled

Dec 15th, 2022
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.16 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Deals with fields used for ordering.
  4.  *
  5.  * First, it allows backend admin ordering.
  6.  * Then, it makes sure numbers in order fields cannot be duplicated, or in wrong sequence.
  7.  * It also deals with delete posts.
  8.  */
  9.  
  10. /**
  11.  * Dynamic Values
  12.  */
  13. $field_slug     = 'wpcf-order';
  14. $post_type_slug = 'features';
  15.  
  16. add_filter( 'manage_' . $post_type_slug . '_posts_columns', 'pp_filter_posts_order_columns' );
  17. add_action( 'manage_' . $post_type_slug . '_posts_custom_column', 'pp_filter_posts_order_column', 10, 2 );
  18. add_filter( 'manage_edit-' . $post_type_slug . '_sortable_columns', 'pp_filter_posts_order_sortable_columns' );
  19. add_action( 'pre_get_posts', 'pp_posts_orderby_order_field' );
  20. add_action( 'wp_trash_post', 'pp_orchestrate_orderby_fields_on_delete' );
  21. add_action( 'save_post', 'pp_orchestrate_orderby_fields', 10, 2 );
  22.  
  23. /**
  24.  * Add order by in WP Admin for order number fields
  25.  */
  26. function pp_filter_posts_order_columns( $columns ) {
  27.     $columns[ $field_slug ] = __( 'Order' );
  28.     return $columns;
  29. }
  30. /**
  31.  * Add order by in WP Admin for order number fields
  32.  */
  33. function pp_filter_posts_order_column( $column, $post_id ) {
  34.     // Image column
  35.     if ( $field_slug === $column ) {
  36.         echo (int) get_post_meta( $post_id, $field_slug, true );
  37.     }
  38. }
  39. /**
  40.  * Add order by in WP Admin for order number fields
  41.  */
  42. function pp_filter_posts_order_sortable_columns( $columns ) {
  43.     $columns[ $field_slug ] = $field_slug;
  44.     return $columns;
  45. }
  46.  
  47. /**
  48.  * Add order by in WP Admin for order number fields
  49.  */
  50. function pp_posts_orderby_order_field( $query ) {
  51.     if ( ! is_admin() || ! $query->is_main_query() ) {
  52.         return;
  53.     }
  54.  
  55.     if ( $field_slug === $query->get( 'orderby' ) ) {
  56.         $query->set( 'orderby', 'meta_value' );
  57.         $query->set( 'meta_key', $field_slug );
  58.         $query->set( 'meta_type', 'numeric' );
  59.     }
  60. }
  61.  
  62. /**
  63.  * Ensure logic to update fields when post is trashed
  64.  *
  65.  * Does not yet deal with "what if removed, but the number actually was wrong" (such as duplicated)
  66.  * Technically this should never happen, because validated on save... but, you know.
  67.  */
  68. function pp_orchestrate_orderby_fields_on_delete( $post_id ) {
  69.  
  70.     // If this is just a revision, don't send the email.
  71.     if ( wp_is_post_revision( $post_id ) ) {
  72.         return;
  73.     }
  74.  
  75.     if ( get_post_type( $post_id ) == $post_type_slug ) {
  76.  
  77.         $args = array(
  78.             'numberposts' => -1,
  79.             'post_type'   => $post_type_slug,
  80.             'fields'      => 'ids',
  81.         );
  82.  
  83.         $all_posts_of_this_type = get_posts( $args );
  84.  
  85.         $removed_value = get_post_meta( $post_id, $field_slug, true );
  86.  
  87.         $all_field_values = array();
  88.         foreach ( $all_posts_of_this_type as  $post_id_available ) {
  89.             $all_field_values[ $post_id_available ] = get_post_meta( $post_id_available, $field_slug, true );
  90.         }
  91.         asort( $all_field_values, SORT_NUMERIC );
  92.  
  93.         ksort( $all_field_values, SORT_NUMERIC );
  94.         $post_id_to_start_from = array_search( $removed_value + 1, $all_field_values );
  95.         $position_in_array     = array_search( $post_id_to_start_from, array_keys( $all_field_values ) );
  96.         $posts_to_update       = array_slice( $all_field_values, $position_in_array, null, true );
  97.         foreach ( $posts_to_update as $post_to_update ) {
  98.             update_post_meta( $post_to_update, $field_slug, $all_field_values[ $post_to_update ] - 1 );
  99.         }
  100.     }
  101.  
  102. }
  103.  
  104. /**
  105.  * Ensure logic to update felds when field a field is updated.
  106.  */
  107. function pp_orchestrate_orderby_fields( $post_id, $post ) {
  108.  
  109.     // If this is just a revision, don't send the email.
  110.     if ( wp_is_post_revision( $post_id ) ) {
  111.         return;
  112.     }
  113.  
  114.     if ( $post->post_type == $post_type_slug ) {
  115.  
  116.         $field_value_added = $_POST[ $field_slug ];
  117.         $args              = array(
  118.             'numberposts' => -1,
  119.             'post_type'   => $post_type_slug,
  120.             'fields'      => 'ids',
  121.         );
  122.  
  123.         $all_posts_of_this_type = get_posts( $args );
  124.         $all_field_values       = array();
  125.         foreach ( $all_posts_of_this_type as  $post_id_available ) {
  126.             $all_field_values[ $post_id_available ] = get_post_meta( $post_id_available, $field_slug, true );
  127.         }
  128.         asort( $all_field_values, SORT_NUMERIC );
  129.  
  130.         /**
  131.          * If number added matches a already used number, it keeps the number and increase all others
  132.          */
  133.         if ( in_array( $field_value_added, $all_field_values ) ) {
  134.             ksort( $all_field_values, SORT_NUMERIC );
  135.             $post_id_to_start_from = array_search( $field_value_added, $all_field_values );
  136.             $position_in_array     = array_search( $post_id_to_start_from, array_keys( $all_field_values ) );
  137.             $posts_to_update       = array_slice( $all_field_values, $position_in_array, null, true );
  138.             foreach ( $posts_to_update as $post_to_update ) {
  139.                 update_post_meta( $post_to_update, $field_slug, $all_field_values[ $post_to_update ] + 1 );
  140.             }
  141.         }
  142.  
  143.         /**
  144.          * If there are missing numbers in the array, it adds this new number in the empty slot
  145.          */
  146.         $missing_number = missing_number( $all_field_values );
  147.         if ( ! empty( $missing_number ) ) {
  148.             asort( $missing_number, SORT_NUMERIC );
  149.             $field_value_added = $missing_number[0];
  150.         }
  151.  
  152.         update_post_meta( $post_id, $field_slug, $field_value_added );
  153.  
  154.     }
  155.  
  156. }
  157.  
  158. function missing_number( $num_list ) {
  159.  
  160.     $new_arr = range( $num_list[0], max( $num_list ) );
  161.  
  162.     return array_diff( $new_arr, $num_list );
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement