summaryrefslogtreecommitdiff
path: root/src/BasicFullCalendar.php
blob: 00d6ee50ac2c77be83ce2530409d26bddf93e9fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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);
    });
}
*/