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
1=== modified file 'functions/event-posts.php'
2--- functions/event-posts.php 2015-06-11 12:42:22 +0000
3+++ functions/event-posts.php 2018-03-21 20:24:37 +0000
4@@ -7,6 +7,9 @@
5 Author: Devin Price
6 Author URI: http://www.wptheming.com
7 License: GPLv2 or later
8+
9+Updated - 21 March 2018 by Peter Mahnke
10+Added to the rest api as /wp-json/wp/v2/event?_embed=True
11 */
12
13 /**
14@@ -48,9 +51,10 @@
15 'capability_type' => 'post',
16 'rewrite' => array("slug" => "event"), // Permalinks format
17 'menu_position' => 5,
18- 'menu_icon' => get_stylesheet_directory_uri() . '/static/img/admin/calendar-day.png',
19+ 'menu_icon' => get_stylesheet_directory_uri() . '/static/img/admin/calendar-day.png',
20+ 'show_in_rest' => true,
21 'has_archive' => true
22- );
23+ );
24
25 register_post_type( 'event', $args );
26 }
27@@ -58,6 +62,40 @@
28 add_action( 'init', 'ep_eventposts' );
29
30 /**
31+ * Updated - 21 March 2018
32+ * add custom fields to rest api
33+**/
34+add_action( 'rest_api_init', 'slug_register_fields' );
35+function slug_register_fields() {
36+ foreach(
37+ array(
38+ '_event_location',
39+ '_event_venue',
40+ '_event_registration',
41+ '_start_day',
42+ '_start_month',
43+ '_start_year',
44+ '_end_day',
45+ '_end_month',
46+ '_end_year'
47+ ) as $field
48+ ) {
49+ register_rest_field( 'event',
50+ $field,
51+ array(
52+ 'get_callback' => 'slug_get_event_data',
53+ 'update_callback' => null,
54+ 'schema' => null,
55+ )
56+ );
57+ }
58+}
59+function slug_get_event_data( $object, $field_name, $request ) {
60+ return get_post_meta( $object[ 'id' ], $field_name, true );
61+}
62+
63+
64+/**
65 * Adds event post metaboxes for start time and end time
66 * http://codex.wordpress.org/Function_Reference/add_meta_box
67 *
68@@ -102,15 +140,15 @@
69 if ( empty( $year ) ) {
70 $year = gmdate( 'Y', $time_adj );
71 }
72-
73+
74 $hour = get_post_meta($post->ID, $metabox_id . '_hour', true);
75-
76+
77 if ( empty($hour) ) {
78 $hour = gmdate( 'H', $time_adj );
79 }
80-
81+
82 $min = get_post_meta($post->ID, $metabox_id . '_minute', true);
83-
84+
85 if ( empty($min) ) {
86 $min = '00';
87 }
88@@ -129,7 +167,7 @@
89 echo '<input type="text" name="' . $metabox_id . '_year" value="' . $year . '" size="4" maxlength="4" /> ';
90 //echo '<input type="text" name="' . $metabox_id . '_hour" value="' . $hour . '" size="2" maxlength="2"/>:';
91 //echo '<input type="text" name="' . $metabox_id . '_minute" value="' . $min . '" size="2" maxlength="2" />';
92-
93+
94 }
95
96 function ept_event_location() {
97@@ -181,11 +219,11 @@
98
99 // OK, we're authenticated: we need to find and save the data
100 // We'll put it into an array to make it easier to loop though
101-
102+
103 $metabox_ids = array( '_start', '_end' );
104
105 foreach ($metabox_ids as $key ) {
106-
107+
108 $aa = $_POST[$key . '_year'];
109 $mm = $_POST[$key . '_month'];
110 $jj = $_POST[$key . '_day'];
111@@ -200,7 +238,7 @@
112 $hh = ($hh > 23 ) ? 23 : $hh;
113 $mn = sprintf('%02d',$mn);
114 $mn = ($mn > 59 ) ? 59 : $mn;
115-
116+
117 $events_meta[$key . '_year'] = $aa;
118 $events_meta[$key . '_month'] = $mm;
119 $events_meta[$key . '_day'] = $jj;
120@@ -208,16 +246,16 @@
121 $events_meta[$key . '_minute'] = $mn;
122 $events_meta[$key . '_eventtimestamp'] = $aa . $mm . $jj . $hh . $mn;
123 }
124-
125+
126 // Save Locations Meta
127-
128- $events_meta['_event_location'] = $_POST['_event_location'];
129+
130+ $events_meta['_event_location'] = $_POST['_event_location'];
131 // Save venue Meta
132-
133- $events_meta['_event_venue'] = $_POST['_event_venue'];
134+
135+ $events_meta['_event_venue'] = $_POST['_event_venue'];
136 // Save registration Meta
137-
138- $events_meta['_event_registration'] = $_POST['_event_registration'];
139+
140+ $events_meta['_event_registration'] = $_POST['_event_registration'];
141 // Add values of $events_meta as custom fields
142
143 foreach ( $events_meta as $key => $value ) { // Cycle through the $events_meta array!
144@@ -240,7 +278,7 @@
145 */
146
147 // Get the Month Abbreviation
148-
149+
150 function eventposttype_get_the_month_abbr($month) {
151 global $wp_locale;
152 for ( $i = 1; $i < 13; $i = $i +1 ) {
153@@ -249,9 +287,9 @@
154 }
155 return $monthabbr;
156 }
157-
158+
159 // Display the date
160-
161+
162 function eventposttype_get_the_event_date() {
163 global $post;
164 $eventdate = '';
165@@ -274,13 +312,13 @@
166
167 /**
168 * Customize Event Query using Post Meta
169- *
170+ *
171 * @link http://www.billerickson.net/customize-the-wordpress-query/
172 * @param object $query data
173 *
174 */
175 function be_event_query( $query ) {
176-
177+
178 if( $query->is_main_query() && !$query->is_feed() && !is_admin() && $query->is_post_type_archive( 'event' ) ) {
179 $meta_query = array(
180 array(
181@@ -300,4 +338,4 @@
182
183 add_action( 'pre_get_posts', 'be_event_query' );
184
185-?>
186\ No newline at end of file
187+?>
188
189=== modified file 'functions/profile.php'
190--- functions/profile.php 2014-02-27 09:41:50 +0000
191+++ functions/profile.php 2018-03-21 20:24:37 +0000
192@@ -19,16 +19,48 @@
193 $contactmethods['location'] = 'Location (Indicate your city and country. Eg. London, United Kingdom)';
194 //add user image
195 //$contactmethods['user_photo'] = 'User photo';
196-
197+
198 return $contactmethods;
199 }
200 add_filter('user_contactmethods','user_profiles',10,1);
201-
202+
203 add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
204 add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
205
206 add_action( 'register_form', 'ts_show_extra_register_fields' );
207
208+/**
209+ * updated - 21 March 2018
210+ * add custom user fields to the user to rest api
211+ * /wp-json/wp/v2/users?_embed=True ...
212+**/
213+add_action( 'rest_api_init', 'user_register_fields' );
214+function user_register_fields() {
215+ foreach( array (
216+ 'user_job_title',
217+ 'user_website_title',
218+ 'user_google',
219+ 'user_twitter',
220+ 'user_facebook',
221+ 'user_photo',
222+ 'user_location'
223+ ) as $field ) {
224+ register_rest_field( 'user',
225+ $field,
226+ array(
227+ 'get_callback' => 'user_get_contact_data',
228+ 'update_callback' => null,
229+ 'schema' => null,
230+ )
231+ );
232+ }
233+ }
234+function user_get_contact_data( $user, $field_name, $request ) {
235+ return get_user_meta( $user['id'], $field_name, true );
236+}
237+
238+
239+
240 function my_show_extra_profile_fields( $user ) { ?>
241 <h3>Extra profile information</h3>
242 <table class="form-table">
243@@ -59,7 +91,7 @@
244 if(current_user_can('edit_posts', $user_id ))
245 update_usermeta( $user_id, 'displayonteam', $_POST['displayonteam'] );
246 update_usermeta( $user_id, 'guestblogger', $_POST['guestblogger'] );
247-}
248+}
249 // end of adding custom fields to WP user profile
250
251 // removing WP user profile fields
252@@ -79,8 +111,8 @@
253 }
254 // end removing WP user profile fields
255
256-// remove user bio html filtering
257+// remove user bio html filtering
258 remove_filter('pre_user_description', 'wp_filter_kses');
259-// end remove user bio html filtering
260+// end remove user bio html filtering
261
262-?>
263\ No newline at end of file
264+?>

Subscribers

People subscribed via source and target branches