diff options
author | Daniel Weipert <code@drogueronin.de> | 2020-12-18 00:56:32 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2020-12-18 00:56:32 +0100 |
commit | 62046b3acde01f9f66e1d5d52e03fea4c5bbfc8d (patch) | |
tree | 576ac2411e8b6f4d3c31b26ce48a2704ba0b5b9e /src |
Diffstat (limited to 'src')
-rw-r--r-- | src/BasicFullCalendar.php | 158 | ||||
-rw-r--r-- | src/PostType.php | 54 |
2 files changed, 212 insertions, 0 deletions
diff --git a/src/BasicFullCalendar.php b/src/BasicFullCalendar.php new file mode 100644 index 0000000..00d6ee5 --- /dev/null +++ b/src/BasicFullCalendar.php @@ -0,0 +1,158 @@ +<?php + +namespace BasicFullCalendar; + +use PostTypes\Metabox; +use PostTypes\MetaboxField; +use PostTypes\Taxonomy; +use \WP_REST_Posts_Controller; + +class BasicFullCalendar +{ + public function __construct() + { + // create post type + $calendarEvent = (new PostType('calendar-event')) + ->icon('dashicons-calendar-alt') + ->textdomain('basic-fullcalendar', dirname(__DIR__) . '/languages/'); + + // load text domain and set label TODO: formulate issues for textdomain loading and reference existing issue: https://github.com/jjgrainger/PostTypes/issues/66 + $calendarEvent->loadTextdomain(); + $calendarEvent->labels(['name' => __($calendarEvent->singular, 'basic-fullcalendar')]); + + // activate REST + $calendarEvent->options([ + 'show_in_rest' => true, + 'supports' => ['title', 'editor', 'custom-fields'], + ]); + + // register + $calendarEvent->register(); + + // filter calendar events by date meta fields instead of post date when doing a date_query via WP API + add_filter("rest_{$calendarEvent->name}_query", function ($args, \WP_REST_Request $request) { + if (! isset($args['date_query'])) { + return $args; + } + + unset($args['date_query']); + $before = date('Y-m-d H:i:s', strtotime($request->get_param('before'))); + $after = date('Y-m-d H:i:s', strtotime($request->get_param('after'))); + $args['meta_query'] = [ + 'relation' => 'OR', + [ + 'key' => 'start_date', + 'value' => [$before, $after], + 'compare' => 'BETWEEN', + 'type' => 'DATETIME', + ], + [ + 'key' => 'end_date', + 'value' => [$before, $after], + 'compare' => 'BETWEEN', + 'type' => 'DATETIME', + ], + ]; + + return $args; + }, 10, 2); + + // add meta data + $meta = new Metabox('Meta'); + $meta->posttype($calendarEvent->name); + $dateTimeFormat = get_option('date_format') . ' ' . get_option('time_format'); + $meta->field([ + (new MetaboxField('start_date'))->type('date_time_picker')->options(['display_format' => $dateTimeFormat, 'return_format' => $dateTimeFormat]), + (new MetaboxField('end_date'))->type('date_time_picker')->options(['display_format' => $dateTimeFormat, 'return_format' => $dateTimeFormat]), + ]); + $meta->add(); + + // register meta fields to use in REST API + add_action('rest_api_init', function () use ($meta, $calendarEvent) { + foreach ($meta->fields as $field) { + register_post_meta($calendarEvent->name, $field->names['name'], [ + 'type' => 'string', + 'single' => true, + 'show_in_rest' => true, + ]); + } + }); + + // add event categories + $calendarEventCategory = new Taxonomy('calendar-event-category'); + $calendarEventCategory->options([ + 'show_in_rest' => true, + ]); + $calendarEventCategory->posttype($calendarEvent->name); + $calendarEventCategory->columns() + ->add(['color' => __('Color')]) + ->order(['color' => 2]) + ->populate('color', function ($content, $column, $term_id) { + $color = get_field('color', "term_$term_id"); + return "<span style='color: $color;'>$color</span>"; + }); + $calendarEventCategory->register(); + + // add color meta to event category + $termMeta = new Metabox('TermMeta'); + $termMeta->taxonomy($calendarEventCategory->name); + $termMeta->field([ + (new MetaboxField('color'))->type('color_picker') + ]); + $termMeta->add(); + + // register meta fields to use in REST API + add_action('rest_api_init', function () use ($termMeta, $calendarEventCategory) { + foreach ($termMeta->fields as $field) { + register_term_meta($calendarEventCategory->name, $field->names['name'], [ + 'type' => 'string', + 'single' => true, + 'show_in_rest' => true, + ]); + } + }); + + /* + add_filter('rest_prepare_' . $calendarEvent->name, function (\WP_REST_Response $response, \WP_Post $post) use ($termMeta) { + $terms = wp_get_object_terms($post->ID, 'calendar-event-category'); + $data = $response->get_data(); + $data['tax_meta'] = []; + foreach ($termMeta->fields as $field) { + $key = $field->names['name']; + $data['tax_meta'][$key] = get_term_meta($terms[0]->term_id, $key, true); + } + $response->set_data($data); + return $response; + }, 10, 2); + */ + + // add shortcode to display the calendar and load the wp-api react stuff + add_shortcode('basic_fullcalendar', function ($atts, $content) { + list('dependencies' => $dependencies, 'version' => $version) = require(dirname(__DIR__) . '/build/index.asset.php'); + $dependencies[] = 'wp-api'; + wp_enqueue_script('basic-fullcalendar', basicFullCalendarAssetsUrl('index.js'), $dependencies, $version); + wp_enqueue_style('basic-fullcalendar', basicFullCalendarAssetsUrl('index.css'), [], $version); + + return '<div id="basic-fullcalendar"></div>'; + }); + } +} + +/* +$addColumns = [ + '_name' => __('Name'), + '_deal_date' => __('Deal time'), + '_is_dealed' => __('Price'), + '_age' => __('Age'), + '_phone' => __('Phone'), + '_price' => __('Price'), +]; + +$client->columns()->add($addColumns); + +foreach ($addColumns as $key => $title) { + $client->columns()->populate($key, function ($column, $post_id) { + echo get_post_meta($post_id, $column, true); + }); +} +*/ diff --git a/src/PostType.php b/src/PostType.php new file mode 100644 index 0000000..67f4fd2 --- /dev/null +++ b/src/PostType.php @@ -0,0 +1,54 @@ +<?php + +namespace BasicFullCalendar; + +class PostType extends \PostTypes\PostType +{ + public string $textdomain = ''; + public string $textdomainDir; + public string $textdomainFile; + public static $defaultTextdomain = 'PostTypes/PostType'; + + public function textdomain($textdomain, $textdomainDir, $textdomainFile = '') + { + $this->textdomain = $textdomain; + $this->textdomainDir = $textdomainDir; + $this->textdomainFile = $textdomainFile ?: get_locale() . '.mo'; + + return $this; + } + + public function register() + { + add_action('init', [$this, 'loadTextdomain']); + parent::register(); + } + + public function loadTextdomain() + { + load_textdomain(self::$defaultTextdomain, dirname(__DIR__) . '/languages/' . get_locale() . '.mo'); + + if (! empty($this->textdomain)) { + load_textdomain($this->textdomain, trailingslashit($this->textdomainDir) . $this->textdomainFile); + } + } + + public function createLabels() + { + return array_replace_recursive([ + 'name' => __($this->plural, $this->textdomain), + 'singular_name' => __($this->singular, $this->textdomain), + 'menu_name' => __($this->plural, $this->textdomain), + 'all_items' => __($this->plural, $this->textdomain), + 'add_new' => __('Add New', self::$defaultTextdomain), + 'add_new_item' => sprintf(__('Add New %s', self::$defaultTextdomain), __($this->singular, $this->textdomain)), + 'edit_item' => sprintf(__('Edit %s', self::$defaultTextdomain), __($this->singular, $this->textdomain)), + 'new_item' => sprintf(__('New %s', self::$defaultTextdomain), __($this->singular, $this->textdomain)), + 'view_item' => sprintf(__('View %s', self::$defaultTextdomain), __($this->singular, $this->textdomain)), + 'search_items' => sprintf(__('Search %s', self::$defaultTextdomain), $this->plural, $this->textdomain), + 'not_found' => sprintf(__('No %s found', self::$defaultTextdomain), __($this->plural, $this->textdomain)), + 'not_found_in_trash' => sprintf(__('No %s found in Trash', self::$defaultTextdomain), __($this->plural, $this->textdomain)), + 'parent_item_colon' => sprintf(__('Parent %s:', self::$defaultTextdomain), __($this->singular, $this->textdomain)), + ], $this->labels); + } +} |