From 62046b3acde01f9f66e1d5d52e03fea4c5bbfc8d Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Fri, 18 Dec 2020 00:56:32 +0100 Subject: Initial commit --- src/BasicFullCalendar.php | 158 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/BasicFullCalendar.php (limited to 'src/BasicFullCalendar.php') 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 @@ +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 "$color"; + }); + $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 '
'; + }); + } +} + +/* +$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); + }); +} +*/ -- cgit v1.2.3