Guest User

Untitled

a guest
Jan 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. /**
  2. * Add our extra HTML
  3. */
  4. add_action( 'export_filters', function() {
  5. ?>
  6. <p><ul id="wpse-post-filters" class="wpse-export-filters">
  7. <li>
  8. <label><?php _e( 'Single day:' ); ?></label>
  9. <select name="wpse_single_day">
  10. <option value="0"><?php _e( 'Select a day' ); ?></option>
  11. <?php wpse_export_single_day_options(); ?>
  12. </select>
  13. </li>
  14. </ul></p>
  15. <?php
  16. });
  17.  
  18. /**
  19. * Modification of the core export_date_options() function
  20. */
  21. function wpse_export_single_day_options( $post_type = 'post' )
  22. {
  23. global $wpdb, $wp_locale;
  24. $months = $wpdb->get_results(
  25. $wpdb->prepare(
  26. "SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month, Day( post_date ) as day
  27. FROM {$wpdb->posts}
  28. WHERE post_type = %s AND post_status != 'auto-draft'
  29. ORDER BY post_date DESC",
  30. $post_type
  31. )
  32. );
  33. $month_count = count( $months );
  34. if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
  35. return;
  36.  
  37. foreach ( $months as $date )
  38. {
  39. if ( 0 == $date->year )
  40. continue;
  41. $month = zeroise( $date->month, 2 );
  42. printf(
  43. '<option value="%d-%d-%d">%d. %s %d</option>',
  44. $date->year,
  45. $month,
  46. $date->day,
  47. $date->day,
  48. $wp_locale->get_month( $month ),
  49. $date->year
  50. );
  51. }
  52. }
  53.  
  54. /**
  55. * Append our HTML to the post filter section
  56. */
  57. add_action( 'admin_head', function()
  58. {?>
  59. <script type="text/javascript">
  60. jQuery(document).ready(function($){
  61. $('#wpse-post-filters').appendTo( $('#post-filters') );
  62. });
  63. </script>
  64. <?php });
  65.  
  66. /**
  67. * Modify the database queries, indirectly
  68. */
  69. add_filter( 'export_args', function( $args )
  70. {
  71. // User input
  72. $date = filter_input( INPUT_GET, 'wpse_single_day' );
  73.  
  74. // Let's use DateTime to validate the Y-m-d input,
  75. // See here http://stackoverflow.com/a/13194441/2078474
  76. $dt = DateTime::createFromFormat( 'Y-m-d', $date );
  77.  
  78. // Check if the user input is a valid date:
  79. if( method_exists( $dt, 'format' ) && $Ymd = $dt->format( 'Y-m-d' ) )
  80. {
  81. // The from date for the db query:
  82. $args['start_date'] = $Ymd;
  83.  
  84. // I think we can modify the end date, in the db query, with this little trick
  85. $args['end_date'] = date(
  86. 'Y-m-d',
  87. strtotime(
  88. '-1 month',
  89. strtotime(
  90. '+1 day',
  91. strtotime( $Ymd )
  92. )
  93. )
  94. );
  95. }
  96. return $args;
  97. });
Add Comment
Please, Sign In to add comment