false ), 'objects' );
$extra_types = get_post_types(
array(
'_builtin' => false,
'can_export' => true,
),
'objects'
);
require AIODIE_PLUGIN_DIR . 'includes/control.php';
}
/**
* Prints month options for a post type.
*
* @param string $post_type Post type name.
* @return void
*/
public function render_month_options( $post_type ) {
foreach ( $this->month_choices( $post_type ) as $value => $label ) {
echo '';
}
}
/**
* Distinct year-month values for a post type, newest first.
*
* @param string $post_type Post type name.
* @return array Map of Y-m values to labels.
*/
private function month_choices( $post_type ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- WordPress database API.
global $wpdb;
$post_type = sanitize_key( $post_type );
if ( ! post_type_exists( $post_type ) ) {
return array();
}
$cache_key = 'aiodie_months_' . $post_type;
$cached = wp_cache_get( $cache_key, 'aiodie' );
if ( is_array( $cached ) ) {
return $cached;
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Distinct months are not available through WP_Query. The result is cached below.
$rows = $wpdb->get_results(
$wpdb->prepare(
'SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month FROM %i WHERE post_type = %s AND post_status != %s ORDER BY year DESC, month DESC LIMIT 240',
$wpdb->posts,
$post_type,
'auto-draft'
)
);
$choices = array();
if ( is_array( $rows ) ) {
foreach ( $rows as $row ) {
$year = isset( $row->year ) ? (int) $row->year : 0;
$month = isset( $row->month ) ? (int) $row->month : 0;
if ( $year < 1970 || $month < 1 || $month > 12 ) {
continue;
}
$value = sprintf( '%04d-%02d', $year, $month );
$choices[ $value ] = wp_date( 'F Y', gmmktime( 0, 0, 0, $month, 1, $year ) );
}
}
wp_cache_set( $cache_key, $choices, 'aiodie', HOUR_IN_SECONDS );
return $choices;
}
}