Created
October 4, 2022 15:33
-
-
Save damiencarbery/d78212756e389b60b6746734c726e36b to your computer and use it in GitHub Desktop.
Show GP Premium Elements by date range - Add CMB2 metabox to show a GP Premium Element in a chosen date range.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Show GP Premium Elements by date range | |
Plugin URI: https://www.damiencarbery.com/2022/10/show-gp-premium-elements-by-date-range/ | |
Description: Add CMB2 metabox to show a GP Premium Element in a chosen date range. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
class ShowGPElementByDate { | |
// Returns an instance of this class. | |
public static function get_instance() { | |
if ( null == self::$instance ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
// Initialize the plugin variables. | |
public function __construct() { | |
$this->init(); | |
} | |
// Set up WordPress specfic actions. | |
public function init() { | |
// Verify that CMB2 plugin is active. | |
add_action( 'admin_notices', array( $this, 'verify_cmb2_active' ) ); | |
// Add the CMB2 metabox. | |
add_action( 'cmb2_admin_init', array( $this, 'gp_elements_metaboxes' ) ); | |
// Retrieve the postmeta to determine whether to display the Element. | |
add_filter( 'generate_element_display', array( $this, 'generate_element_display' ), 10, 2 ); | |
} | |
public function gp_elements_metaboxes() { | |
$cmb = new_cmb2_box( array( | |
'id' => 'gpe_dates', | |
'title' => 'Show Date Range', | |
'object_types' => array( 'gp_elements', ), // GP Premium Elements | |
) ); | |
$cmb->add_field( array( | |
'name' => 'Enter start and end dates', | |
'desc' => 'Set the date range to show this element. Clear the fields to always show the element (considering Display Rules).', | |
'type' => 'title', | |
'id' => 'gpe_dates_title' | |
) ); | |
$cmb->add_field( array( | |
'name' => 'Start Date', | |
'desc' => 'When to start showing this element', | |
'id' => 'element_start_date', | |
'type' => 'text_date_timestamp', | |
'date_format' => 'j M Y', | |
) ); | |
$cmb->add_field( array( | |
'name' => 'End Date', | |
'desc' => 'When to stop showing this element', | |
'id' => 'element_end_date', | |
'type' => 'text_date_timestamp', | |
'date_format' => 'j M Y', | |
) ); | |
} | |
public function generate_element_display( $display, $post_id ) { | |
//error_log( 'GED $post_id: ' . $post_id ); | |
//error_log( 'GED Element title: ' . get_the_title( $post_id ) ); | |
if ( is_admin() ) { return $display; } | |
$start_date = get_post_meta( $post_id, 'element_start_date', true ); | |
$end_date = get_post_meta( $post_id, 'element_end_date', true ); | |
//error_log( 'Start date: ' . var_export( $start_date, true ) ); | |
//error_log( 'End date: ' . var_export( $end_date, true ) ); | |
// Show this GP Element between the two dates. | |
if ( !is_null( $start_date ) && !is_null( $end_date ) ) { | |
$now = time(); | |
//error_log( 'Now: ' . var_export( $now, true ) ); | |
if ( $now >= $start_date && $now <= $end_date ) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
return $display; | |
} | |
// Verify that CMB2 plugin is active. | |
public function verify_cmb2_active() { | |
if ( ! defined( 'CMB2_LOADED' ) ) { | |
$plugin_data = get_plugin_data( __FILE__ ); | |
$plugin_name = $plugin_data['Name']; | |
?> | |
<div class="notice notice-warning is-dismissible"><p>Plugin <strong><?php echo $plugin_name; ?></strong> requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a>.</p></div> | |
<?php | |
//error_log( 'CMB2 is not active.' ); | |
} | |
} | |
} | |
$ShowGPElementByDate = new ShowGPElementByDate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment