This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucla-dcp.php
177 lines (163 loc) · 4.81 KB
/
ucla-dcp.php
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Plugin Name: UCLA-WP Plugin
* Description: UCLA branded and ADA complaint components. Currently in beta, not reccomended for production use.
* Version: 0.1.1
* Author: Computing and Disabilities Program and Strategic Communications
* Text Domain: ucla-wp-plugin
*
* @package uwai
*/
/**
* Registers all block assets so that they can be enqueued through the block editor
* in the corresponding context.
*
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/
*/
/* require 'libs/ucla-updater/updater.php';
new PluginUpdater(__FILE__, 'avelikanov/testpg', 'master'); */
include __DIR__ . '/src/event-card/index.php';
include __DIR__ . '/src/recent-posts/index.php';
include __DIR__ . '/src/gallery-block/index.php';
include __DIR__ . '/src/publications/index.php';
function ucla_dcp_ucla_dcp_block_init() {
wp_enqueue_script('jquery');
$dir = dirname( __FILE__ );
$script_asset_path = "$dir/build/index.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "uwai/uwai" block first.'
);
}
$index_js = 'build/index.js';
$script_asset = require( $script_asset_path );
wp_register_script(
'uwai-uwai-block-editor',
plugins_url( $index_js, __FILE__ ),
$script_asset['dependencies'],
$script_asset['version']
);
wp_localize_script(
'uwai-uwai-block-editor',
'js_data',
array(
'path' => plugins_url( '', __FILE__ )
)
);
$editor_css = 'build/index.css';
wp_register_style(
'uwai-uwai-block-editor',
plugins_url( $editor_css, __FILE__ ),
array(),
filemtime( "$dir/$editor_css" )
);
$style_css = 'build/style-index.css';
wp_register_style(
'uwai-uwai-block',
plugins_url( $style_css, __FILE__ ),
array(),
filemtime( "$dir/$style_css" )
);
register_block_type( 'uwai/uwai', array(
'editor_script' => 'uwai-uwai-block-editor',
'editor_style' => 'uwai-uwai-block-editor',
'style' => 'uwai-uwai-block'
) );
}
add_action( 'init', 'ucla_dcp_ucla_dcp_block_init' );
// Add to API
function register_rest_fields() {
$publication_exists = post_type_exists('publication');
$events_exists = post_type_exists('events');
if ($publication_exists) {
register_rest_field('publication', 'publication_author',
array(
'get_callback' => 'get_post_meta_callback',
'update_callback' => 'update_post_meta_callback',
'schema' => array(
'type' => 'string',
'arg_options' => array(
'sanitize_callback' => function ( $value ) {
// Make the value safe for storage.
return sanitize_text_field( $value );
}
),
),
)
);
}
if ($events_exists) {
register_rest_field('events', 'event_start_date',
array(
'get_callback' => 'get_post_meta_callback',
'update_callback' => 'update_post_meta_callback',
'schema' => array(
'type' => 'string',
'arg_options' => array(
'sanitize_callback' => function ( $value ) {
// Make the value safe for storage.
return sanitize_text_field( $value );
}
),
),
)
);
register_rest_field('events', 'event_end_date',
array(
'get_callback' => 'get_post_meta_callback',
'update_callback' => 'update_post_meta_callback',
'schema' => array(
'type' => 'string',
'arg_options' => array(
'sanitize_callback' => function ( $value ) {
// Make the value safe for storage.
return sanitize_text_field( $value );
}
),
),
)
);
register_rest_field('events', 'event_time',
array(
'get_callback' => 'get_post_meta_callback',
'update_callback' => 'update_post_meta_callback',
'schema' => array(
'type' => 'string',
'arg_options' => array(
'sanitize_callback' => function ( $value ) {
// Make the value safe for storage.
return sanitize_text_field( $value );
}
),
),
)
);
register_rest_field('events', 'event_location',
array(
'get_callback' => 'get_post_meta_callback',
'update_callback' => 'update_post_meta_callback',
'schema' => array(
'type' => 'string',
'arg_options' => array(
'sanitize_callback' => function ( $value ) {
// Make the value safe for storage.
return sanitize_text_field( $value );
}
),
),
)
);
}
}
function get_post_meta_callback($object, $field_name, $request) {
$post_meta = get_post_meta($object['id'], $field_name, true);
$output['rendered'] = $post_meta;
return $output;
}
function update_post_meta_callback($value, $object, $field_name) {
if ( ! $value || ! is_string( $value ) ) {
return;
}
return update_post_meta( $object->ID, $field_name, $value );
}
add_action('rest_api_init', 'register_rest_fields');