*/ class MEC_feature_colors extends MEC_base { /** * @var MEC_factory */ public $factory; /** * @var MEC_main */ public $main; /** * Constructor method * @author Webnus */ public function __construct() { // Import MEC Factory $this->factory = $this->getFactory(); // Import MEC Main $this->main = $this->getMain(); } /** * Initialize colors feature * @author Webnus */ public function init() { $this->factory->action('add_meta_boxes', array($this, 'register_meta_boxes')); $this->factory->action('save_post', array($this, 'save_event'), 3); } /** * Registers color meta box * @author Webnus */ public function register_meta_boxes() { add_meta_box('mec_metabox_color', esc_html__('Event Color', 'modern-events-calendar-lite'), array($this, 'meta_box_colors'), $this->main->get_main_post_type(), 'side'); } public function mec_hex2rgb($hex) { $hex = str_replace("#", "", $hex); if(strlen($hex) == 3) { $r = hexdec(substr($hex,0,1).substr($hex,0,1)); $g = hexdec(substr($hex,1,1).substr($hex,1,1)); $b = hexdec(substr($hex,2,1).substr($hex,2,1)); } else { $r = hexdec(substr($hex,0,2)); $g = hexdec(substr($hex,2,2)); $b = hexdec(substr($hex,4,2)); } return array($r, $g, $b); } /** * Show color meta box content * @author Webnus * @param object $post */ public function meta_box_colors($post) { $color = get_post_meta($post->ID, 'mec_color', true); $available_colors = $this->main->get_available_colors(); ?>
mec_hex2rgb('#'.$available_color); ?> '; ?>
* @param int $post_id * @return void */ public function save_event($post_id) { // Check if our nonce is set. if(!isset($_POST['mec_event_nonce'])) return; // Verify that the nonce is valid. if(!wp_verify_nonce(sanitize_text_field($_POST['mec_event_nonce']), 'mec_event_data')) return; // If this is an autosave, our form has not been submitted, so we don't want to do anything. if(defined('DOING_AUTOSAVE') and DOING_AUTOSAVE) return; // Get Modern Events Calendar Data $_mec = isset($_POST['mec']) ? $this->main->sanitize_deep_array($_POST['mec']) : []; $color = isset($_mec['color']) ? trim(sanitize_text_field($_mec['color']), '# ') : ''; update_post_meta($post_id, 'mec_color', $color); // Add the new color to available colors if(trim($color)) $this->main->add_to_available_colors($color); } }