Merge lp:~peterm-ubuntu/resource-centre/rest-fix into lp:resource-centre

Proposed by Peter Mahnke
Status: Merged
Approved by: Anthony Dillon
Approved revision: 107
Merged at revision: 107
Proposed branch: lp:~peterm-ubuntu/resource-centre/rest-fix
Merge into: lp:resource-centre
Diff against target: 264 lines (+99/-29)
2 files modified
functions/event-posts.php (+61/-23)
functions/profile.php (+38/-6)
To merge this branch: bzr merge lp:~peterm-ubuntu/resource-centre/rest-fix
Reviewer Review Type Date Requested Status
Anthony Dillon Approve
Review via email: mp+341853@code.launchpad.net

Description of the change

## Done

- Created the /event rest api endpoint with custom fields
- Added custom user fields to the /users rest api endpoint

## QA

- Check out this branch locally, or try on insights.canonicalwebteam.com
- View the api in your web browser at:
  - [event](https://insights.canonicalwebteam.com/wp-json/wp/v2/event?_embed=True&id=6180)
    - see that the endpoint exists and the custom fields are there:
```
    "_event_location": "Vancouver, BC",
    "_event_venue": "Vancouver Convention Center",
    "_event_registration": "https://www.openstack.org/summit/vancouver-2015/",
    "_start_day": "18",
    "_start_month": "05",
    "_start_year": "2015",
    "_end_day": "22",
    "_end_month": "06",
    "_end_year": "2016",
```
  - [user](https://insights.canonicalwebteam.com/wp-json/wp/v2/users?_embed&slug=john-zannos)
    - see that the custom fields are there:
```
    "user_job_title": "Vice President of Cloud Alliances",
    "user_website_title": "",
    "user_google": "102884954828153486904",
    "user_twitter": "",
    "user_facebook": "",
    "user_photo": "/wp-content/uploads/index.jpg",
    "user_location": "",
```

To post a comment you must log in.
Revision history for this message
Anthony Dillon (ya-bo-ng) wrote :

Great stuff. Although it seems the `id` does not filter the feed by the single post. For example: http://insights.canonicalwebteam.com/wp-json/wp/v2/event?_embed=True&id=6180 returns all the events not just the data for the 6180 event.

I think we need some logic in the event function to return all or a single post if the id exists. Also the events feed takes a long time. I think we need to limit the results.

Revision history for this message
Anthony Dillon (ya-bo-ng) :
review: Needs Fixing
Revision history for this message
Anthony Dillon (ya-bo-ng) wrote :

After discussing on IRC we will use the slug to return details of events and use per_page by default to limit the returned feed.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'functions/event-posts.php'
--- functions/event-posts.php 2015-06-11 12:42:22 +0000
+++ functions/event-posts.php 2018-03-21 20:24:37 +0000
@@ -7,6 +7,9 @@
7Author: Devin Price7Author: Devin Price
8Author URI: http://www.wptheming.com8Author URI: http://www.wptheming.com
9License: GPLv2 or later9License: GPLv2 or later
10
11Updated - 21 March 2018 by Peter Mahnke
12Added to the rest api as /wp-json/wp/v2/event?_embed=True
10*/13*/
1114
12/**15/**
@@ -48,9 +51,10 @@
48 'capability_type' => 'post',51 'capability_type' => 'post',
49 'rewrite' => array("slug" => "event"), // Permalinks format52 'rewrite' => array("slug" => "event"), // Permalinks format
50 'menu_position' => 5,53 'menu_position' => 5,
51 'menu_icon' => get_stylesheet_directory_uri() . '/static/img/admin/calendar-day.png',54 'menu_icon' => get_stylesheet_directory_uri() . '/static/img/admin/calendar-day.png',
55 'show_in_rest' => true,
52 'has_archive' => true56 'has_archive' => true
53 ); 57 );
5458
55 register_post_type( 'event', $args );59 register_post_type( 'event', $args );
56}60}
@@ -58,6 +62,40 @@
58add_action( 'init', 'ep_eventposts' );62add_action( 'init', 'ep_eventposts' );
5963
60/**64/**
65 * Updated - 21 March 2018
66 * add custom fields to rest api
67**/
68add_action( 'rest_api_init', 'slug_register_fields' );
69function slug_register_fields() {
70 foreach(
71 array(
72 '_event_location',
73 '_event_venue',
74 '_event_registration',
75 '_start_day',
76 '_start_month',
77 '_start_year',
78 '_end_day',
79 '_end_month',
80 '_end_year'
81 ) as $field
82 ) {
83 register_rest_field( 'event',
84 $field,
85 array(
86 'get_callback' => 'slug_get_event_data',
87 'update_callback' => null,
88 'schema' => null,
89 )
90 );
91 }
92}
93function slug_get_event_data( $object, $field_name, $request ) {
94 return get_post_meta( $object[ 'id' ], $field_name, true );
95}
96
97
98/**
61 * Adds event post metaboxes for start time and end time99 * Adds event post metaboxes for start time and end time
62 * http://codex.wordpress.org/Function_Reference/add_meta_box100 * http://codex.wordpress.org/Function_Reference/add_meta_box
63 *101 *
@@ -102,15 +140,15 @@
102 if ( empty( $year ) ) {140 if ( empty( $year ) ) {
103 $year = gmdate( 'Y', $time_adj );141 $year = gmdate( 'Y', $time_adj );
104 }142 }
105 143
106 $hour = get_post_meta($post->ID, $metabox_id . '_hour', true);144 $hour = get_post_meta($post->ID, $metabox_id . '_hour', true);
107 145
108 if ( empty($hour) ) {146 if ( empty($hour) ) {
109 $hour = gmdate( 'H', $time_adj );147 $hour = gmdate( 'H', $time_adj );
110 }148 }
111 149
112 $min = get_post_meta($post->ID, $metabox_id . '_minute', true);150 $min = get_post_meta($post->ID, $metabox_id . '_minute', true);
113 151
114 if ( empty($min) ) {152 if ( empty($min) ) {
115 $min = '00';153 $min = '00';
116 }154 }
@@ -129,7 +167,7 @@
129 echo '<input type="text" name="' . $metabox_id . '_year" value="' . $year . '" size="4" maxlength="4" /> ';167 echo '<input type="text" name="' . $metabox_id . '_year" value="' . $year . '" size="4" maxlength="4" /> ';
130 //echo '<input type="text" name="' . $metabox_id . '_hour" value="' . $hour . '" size="2" maxlength="2"/>:';168 //echo '<input type="text" name="' . $metabox_id . '_hour" value="' . $hour . '" size="2" maxlength="2"/>:';
131 //echo '<input type="text" name="' . $metabox_id . '_minute" value="' . $min . '" size="2" maxlength="2" />';169 //echo '<input type="text" name="' . $metabox_id . '_minute" value="' . $min . '" size="2" maxlength="2" />';
132 170
133}171}
134172
135function ept_event_location() {173function ept_event_location() {
@@ -181,11 +219,11 @@
181219
182 // OK, we're authenticated: we need to find and save the data220 // OK, we're authenticated: we need to find and save the data
183 // We'll put it into an array to make it easier to loop though221 // We'll put it into an array to make it easier to loop though
184 222
185 $metabox_ids = array( '_start', '_end' );223 $metabox_ids = array( '_start', '_end' );
186224
187 foreach ($metabox_ids as $key ) {225 foreach ($metabox_ids as $key ) {
188 226
189 $aa = $_POST[$key . '_year'];227 $aa = $_POST[$key . '_year'];
190 $mm = $_POST[$key . '_month'];228 $mm = $_POST[$key . '_month'];
191 $jj = $_POST[$key . '_day'];229 $jj = $_POST[$key . '_day'];
@@ -200,7 +238,7 @@
200 $hh = ($hh > 23 ) ? 23 : $hh;238 $hh = ($hh > 23 ) ? 23 : $hh;
201 $mn = sprintf('%02d',$mn);239 $mn = sprintf('%02d',$mn);
202 $mn = ($mn > 59 ) ? 59 : $mn;240 $mn = ($mn > 59 ) ? 59 : $mn;
203 241
204 $events_meta[$key . '_year'] = $aa;242 $events_meta[$key . '_year'] = $aa;
205 $events_meta[$key . '_month'] = $mm;243 $events_meta[$key . '_month'] = $mm;
206 $events_meta[$key . '_day'] = $jj;244 $events_meta[$key . '_day'] = $jj;
@@ -208,16 +246,16 @@
208 $events_meta[$key . '_minute'] = $mn;246 $events_meta[$key . '_minute'] = $mn;
209 $events_meta[$key . '_eventtimestamp'] = $aa . $mm . $jj . $hh . $mn;247 $events_meta[$key . '_eventtimestamp'] = $aa . $mm . $jj . $hh . $mn;
210 }248 }
211 249
212 // Save Locations Meta250 // Save Locations Meta
213 251
214 $events_meta['_event_location'] = $_POST['_event_location']; 252 $events_meta['_event_location'] = $_POST['_event_location'];
215 // Save venue Meta253 // Save venue Meta
216 254
217 $events_meta['_event_venue'] = $_POST['_event_venue']; 255 $events_meta['_event_venue'] = $_POST['_event_venue'];
218 // Save registration Meta256 // Save registration Meta
219 257
220 $events_meta['_event_registration'] = $_POST['_event_registration']; 258 $events_meta['_event_registration'] = $_POST['_event_registration'];
221 // Add values of $events_meta as custom fields259 // Add values of $events_meta as custom fields
222260
223 foreach ( $events_meta as $key => $value ) { // Cycle through the $events_meta array!261 foreach ( $events_meta as $key => $value ) { // Cycle through the $events_meta array!
@@ -240,7 +278,7 @@
240 */278 */
241279
242// Get the Month Abbreviation280// Get the Month Abbreviation
243 281
244function eventposttype_get_the_month_abbr($month) {282function eventposttype_get_the_month_abbr($month) {
245 global $wp_locale;283 global $wp_locale;
246 for ( $i = 1; $i < 13; $i = $i +1 ) {284 for ( $i = 1; $i < 13; $i = $i +1 ) {
@@ -249,9 +287,9 @@
249 }287 }
250 return $monthabbr;288 return $monthabbr;
251}289}
252 290
253// Display the date291// Display the date
254 292
255function eventposttype_get_the_event_date() {293function eventposttype_get_the_event_date() {
256 global $post;294 global $post;
257 $eventdate = '';295 $eventdate = '';
@@ -274,13 +312,13 @@
274312
275/**313/**
276 * Customize Event Query using Post Meta314 * Customize Event Query using Post Meta
277 * 315 *
278 * @link http://www.billerickson.net/customize-the-wordpress-query/316 * @link http://www.billerickson.net/customize-the-wordpress-query/
279 * @param object $query data317 * @param object $query data
280 *318 *
281 */319 */
282function be_event_query( $query ) {320function be_event_query( $query ) {
283 321
284 if( $query->is_main_query() && !$query->is_feed() && !is_admin() && $query->is_post_type_archive( 'event' ) ) {322 if( $query->is_main_query() && !$query->is_feed() && !is_admin() && $query->is_post_type_archive( 'event' ) ) {
285 $meta_query = array(323 $meta_query = array(
286 array(324 array(
@@ -300,4 +338,4 @@
300338
301add_action( 'pre_get_posts', 'be_event_query' );339add_action( 'pre_get_posts', 'be_event_query' );
302340
303?>
304\ No newline at end of file341\ No newline at end of file
342?>
305343
=== modified file 'functions/profile.php'
--- functions/profile.php 2014-02-27 09:41:50 +0000
+++ functions/profile.php 2018-03-21 20:24:37 +0000
@@ -19,16 +19,48 @@
19 $contactmethods['location'] = 'Location (Indicate your city and country. Eg. London, United Kingdom)';19 $contactmethods['location'] = 'Location (Indicate your city and country. Eg. London, United Kingdom)';
20 //add user image20 //add user image
21 //$contactmethods['user_photo'] = 'User photo';21 //$contactmethods['user_photo'] = 'User photo';
22 22
23 return $contactmethods;23 return $contactmethods;
24 }24 }
25 add_filter('user_contactmethods','user_profiles',10,1);25 add_filter('user_contactmethods','user_profiles',10,1);
26 26
27add_action( 'show_user_profile', 'my_show_extra_profile_fields' );27add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
28add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );28add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
2929
30add_action( 'register_form', 'ts_show_extra_register_fields' );30add_action( 'register_form', 'ts_show_extra_register_fields' );
3131
32/**
33 * updated - 21 March 2018
34 * add custom user fields to the user to rest api
35 * /wp-json/wp/v2/users?_embed=True ...
36**/
37add_action( 'rest_api_init', 'user_register_fields' );
38function user_register_fields() {
39 foreach( array (
40 'user_job_title',
41 'user_website_title',
42 'user_google',
43 'user_twitter',
44 'user_facebook',
45 'user_photo',
46 'user_location'
47 ) as $field ) {
48 register_rest_field( 'user',
49 $field,
50 array(
51 'get_callback' => 'user_get_contact_data',
52 'update_callback' => null,
53 'schema' => null,
54 )
55 );
56 }
57 }
58function user_get_contact_data( $user, $field_name, $request ) {
59 return get_user_meta( $user['id'], $field_name, true );
60}
61
62
63
32function my_show_extra_profile_fields( $user ) { ?>64function my_show_extra_profile_fields( $user ) { ?>
33<h3>Extra profile information</h3>65<h3>Extra profile information</h3>
34<table class="form-table">66<table class="form-table">
@@ -59,7 +91,7 @@
59 if(current_user_can('edit_posts', $user_id ))91 if(current_user_can('edit_posts', $user_id ))
60 update_usermeta( $user_id, 'displayonteam', $_POST['displayonteam'] );92 update_usermeta( $user_id, 'displayonteam', $_POST['displayonteam'] );
61 update_usermeta( $user_id, 'guestblogger', $_POST['guestblogger'] );93 update_usermeta( $user_id, 'guestblogger', $_POST['guestblogger'] );
62} 94}
63// end of adding custom fields to WP user profile95// end of adding custom fields to WP user profile
6496
65// removing WP user profile fields97// removing WP user profile fields
@@ -79,8 +111,8 @@
79}111}
80// end removing WP user profile fields112// end removing WP user profile fields
81113
82// remove user bio html filtering 114// remove user bio html filtering
83remove_filter('pre_user_description', 'wp_filter_kses');115remove_filter('pre_user_description', 'wp_filter_kses');
84// end remove user bio html filtering 116// end remove user bio html filtering
85117
86?>
87\ No newline at end of file118\ No newline at end of file
119?>

Subscribers

People subscribed via source and target branches