Skip to content

Commit 4e92574

Browse files
authored
Merge pull request #557 from Automattic/master
Release Jul 28
2 parents 645a15f + 5816ef9 commit 4e92574

File tree

6 files changed

+154
-55
lines changed

6 files changed

+154
-55
lines changed

class-newspack-blocks-api.php

+74
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,31 @@ public static function register_video_playlist_endpoint() {
275275
);
276276
}
277277

278+
/**
279+
* Register specific posts endpoint.
280+
*/
281+
public static function register_post_lookup_endpoint() {
282+
if ( ! Newspack_Blocks::use_experimental() ) {
283+
return;
284+
}
285+
register_rest_route(
286+
'newspack-blocks/v1',
287+
'/specific-posts',
288+
[
289+
'methods' => \WP_REST_Server::READABLE,
290+
'callback' => [ 'Newspack_Blocks_API', 'specific_posts_endpoint' ],
291+
'args' => [
292+
'search' => [
293+
'sanitize_callback' => 'sanitize_text_field',
294+
],
295+
'per_page' => [
296+
'sanitize_callback' => 'absint',
297+
],
298+
],
299+
]
300+
);
301+
}
302+
278303
/**
279304
* Process requests to the video-playlist endpoint.
280305
*
@@ -286,6 +311,54 @@ public static function video_playlist_endpoint( $request ) {
286311
return new \WP_REST_Response( newspack_blocks_get_video_playlist( $args ), 200 );
287312
}
288313

314+
/**
315+
* Lookup individual posts by title only.
316+
*
317+
* @param WP_REST_Request $request Request object.
318+
* @return WP_REST_Response.
319+
*/
320+
public static function specific_posts_endpoint( $request ) {
321+
$params = $request->get_params();
322+
if ( empty( $params['search'] ) ) {
323+
return new \WP_REST_Response( [] );
324+
}
325+
add_filter( 'posts_where', [ 'Newspack_Blocks_API', 'add_post_title_wildcard_search' ], 10, 2 );
326+
327+
$args = [
328+
'post_type' => 'post',
329+
'post_status' => 'publish',
330+
'title_wildcard_search' => esc_sql( $params['search'] ),
331+
'posts_per_page' => $params['per_page'],
332+
];
333+
334+
$query = new WP_Query( $args );
335+
remove_filter( 'posts_where', [ 'Newspack_Blocks_API', 'add_post_title_wildcard_search' ], 10, 2 );
336+
return new \WP_REST_Response(
337+
array_map(
338+
function( $post ) {
339+
return [
340+
'id' => $post->ID,
341+
'title' => $post->post_title,
342+
];
343+
},
344+
$query->posts
345+
),
346+
200
347+
);
348+
}
349+
350+
/**
351+
* Add title wildcard search to post lookup query.
352+
*
353+
* @param String $where Where clause.
354+
* @param WP_Query $query The query.
355+
*/
356+
public static function add_post_title_wildcard_search( $where, $query ) {
357+
$search = ! empty( $query->query['title_wildcard_search'] ) ? $query->query['title_wildcard_search'] : null;
358+
$where .= ' AND post_title LIKE "%' . $search . '%" ';
359+
return $where;
360+
}
361+
289362
/**
290363
* Adds meta query support to API rest endpoint.
291364
*
@@ -315,4 +388,5 @@ public static function post_meta_request_params( $args, $request ) {
315388

316389
add_action( 'rest_api_init', array( 'Newspack_Blocks_API', 'register_rest_fields' ) );
317390
add_action( 'rest_api_init', array( 'Newspack_Blocks_API', 'register_video_playlist_endpoint' ) );
391+
add_action( 'rest_api_init', array( 'Newspack_Blocks_API', 'register_post_lookup_endpoint' ) );
318392
add_filter( 'rest_post_query', array( 'Newspack_Blocks_API', 'post_meta_request_params' ), 10, 2 );

class-newspack-blocks.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public static function enqueue_block_editor_assets() {
6161
'newspack-blocks-editor',
6262
'newspack_blocks_data',
6363
[
64-
'patterns' => self::get_patterns_for_post_type( get_post_type() ),
64+
'patterns' => self::get_patterns_for_post_type( get_post_type() ),
65+
'_experimental' => self::use_experimental(),
6566
]
6667
);
6768

@@ -500,5 +501,14 @@ public static function get_patterns_for_post_type( $post_type = null ) {
500501
}
501502
return $clean;
502503
}
504+
505+
/**
506+
* Whether to use experimental features.
507+
*
508+
* @return bool Experimental status.
509+
*/
510+
public static function use_experimental() {
511+
return defined( 'NEWSPACK_BLOCKS_EXPERIMENTAL' ) && NEWSPACK_BLOCKS_EXPERIMENTAL;
512+
}
503513
}
504514
Newspack_Blocks::init();

src/blocks/donate/edit.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ class Edit extends Component {
158158
<form>
159159
<div className="wp-block-newspack-blocks-donate__options">
160160
{ Object.keys( frequencies ).map( frequencySlug => (
161-
<div className="wp-block-newspack-blocks-donate__frequency" key={ frequencySlug }>
161+
<div
162+
className="wp-block-newspack-blocks-donate__frequency frequency"
163+
key={ frequencySlug }
164+
>
162165
<input
163166
type="radio"
164167
onClick={ () => this.setState( { selectedFrequency: frequencySlug } ) }
@@ -168,7 +171,7 @@ class Edit extends Component {
168171
/>
169172
<label
170173
htmlFor={ 'newspack-donate-' + frequencySlug + '-' + uid }
171-
className="donation-frequency-label"
174+
className="donation-frequency-label freq-label"
172175
>
173176
{ frequencies[ frequencySlug ] }
174177
</label>
@@ -179,7 +182,7 @@ class Edit extends Component {
179182
>
180183
{ __( 'Donation amount', 'newspack-blocks' ) }
181184
</label>
182-
<div className="wp-block-newspack-blocks-donate__money-input">
185+
<div className="wp-block-newspack-blocks-donate__money-input money-input">
183186
<span className="currency">{ currencySymbol }</span>
184187
<input
185188
type="number"
@@ -194,7 +197,7 @@ class Edit extends Component {
194197
</div>
195198
) ) }
196199
</div>
197-
<p className="wp-block-newspack-blocks-donate__thanks">
200+
<p className="wp-block-newspack-blocks-donate__thanks thanks">
198201
{ __( 'Your contribution is appreciated.', 'newspack-blocks' ) }
199202
</p>
200203
<button type="submit" onClick={ evt => evt.preventDefault() }>
@@ -231,9 +234,12 @@ class Edit extends Component {
231234
<div className={ classNames( className, 'tiered wpbnbd' ) }>
232235
<form>
233236
<div className="wp-block-newspack-blocks-donate__options">
234-
<div className="wp-block-newspack-blocks-donate__frequencies">
237+
<div className="wp-block-newspack-blocks-donate__frequencies frequencies">
235238
{ Object.keys( frequencies ).map( frequencySlug => (
236-
<div className="wp-block-newspack-blocks-donate__frequency" key={ frequencySlug }>
239+
<div
240+
className="wp-block-newspack-blocks-donate__frequency frequency"
241+
key={ frequencySlug }
242+
>
237243
<input
238244
type="radio"
239245
onClick={ () => this.setState( { selectedFrequency: frequencySlug } ) }
@@ -243,12 +249,12 @@ class Edit extends Component {
243249
/>
244250
<label
245251
htmlFor={ 'newspack-donate-' + frequencySlug + '-' + uid }
246-
className="donation-frequency-label"
252+
className="donation-frequency-label freq-label"
247253
>
248254
{ frequencies[ frequencySlug ] }
249255
</label>
250256

251-
<div className="wp-block-newspack-blocks-donate__tiers">
257+
<div className="wp-block-newspack-blocks-donate__tiers tiers">
252258
{ suggestedAmounts.map( ( suggestedAmount, index ) => (
253259
<div className="wp-block-newspack-blocks-donate__tier" key={ index }>
254260
<input
@@ -258,7 +264,7 @@ class Edit extends Component {
258264
checked={ index === activeTier }
259265
/>
260266
<label
261-
className="tier-select-label"
267+
className="tier-select-label tier-label"
262268
htmlFor={ 'newspack-tier-' + frequencySlug + '-' + uid + '-' + index }
263269
>
264270
{ this.formatCurrency(
@@ -279,18 +285,18 @@ class Edit extends Component {
279285
checked={ 'other' === activeTier }
280286
/>
281287
<label
282-
className="tier-select-label"
288+
className="tier-select-label tier-label"
283289
htmlFor={ 'newspack-tier-' + frequencySlug + '-' + uid + '-other' }
284290
>
285291
{ __( 'Other', 'newspack-blocks' ) }
286292
</label>
287293
<label
288-
className="other-donate-label"
294+
className="other-donate-label odl"
289295
htmlFor={ 'newspack-tier-' + frequencySlug + '-' + uid + '-other-input' }
290296
>
291297
{ __( 'Donation amount', 'newspack-blocks' ) }
292298
</label>
293-
<div className="wp-block-newspack-blocks-donate__money-input">
299+
<div className="wp-block-newspack-blocks-donate__money-input money-input">
294300
<span className="currency">{ currencySymbol }</span>
295301
<input
296302
type="number"
@@ -305,7 +311,7 @@ class Edit extends Component {
305311
) ) }
306312
</div>
307313
</div>
308-
<p className="wp-block-newspack-blocks-donate__thanks">
314+
<p className="wp-block-newspack-blocks-donate__thanks thanks">
309315
{ __( 'Your contribution is appreciated.', 'newspack-blocks' ) }
310316
</p>
311317
<button type="submit" onClick={ evt => evt.preventDefault() }>

src/blocks/donate/view.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function newspack_blocks_render_block_donate( $attributes ) {
6464
$formatted_amount = number_format( $amount, floatval( $amount ) - intval( $amount ) ? 2 : 0 );
6565
?>
6666

67-
<div class='wp-block-newspack-blocks-donate__frequency'>
67+
<div class='wp-block-newspack-blocks-donate__frequency frequency'>
6868
<input
6969
type='radio'
7070
value='<?php echo esc_attr( $frequency_slug ); ?>'
@@ -74,7 +74,7 @@ function newspack_blocks_render_block_donate( $attributes ) {
7474
/>
7575
<label
7676
for='newspack-donate-<?php echo esc_attr( $frequency_slug . '-' . $uid ); ?>'
77-
class='donation-frequency-label'
77+
class='donation-frequency-label freq-label'
7878
>
7979
<?php echo esc_html( $frequency_name ); ?>
8080
</label>
@@ -85,7 +85,7 @@ class='donate-label'
8585
>
8686
<?php echo esc_html__( 'Donation amount', 'newspack-blocks' ); ?>
8787
</label>
88-
<div class='wp-block-newspack-blocks-donate__money-input'>
88+
<div class='wp-block-newspack-blocks-donate__money-input money-input'>
8989
<span class='currency'>
9090
<?php echo esc_html( $settings['currencySymbol'] ); ?>
9191
</span>
@@ -100,7 +100,7 @@ class='donate-label'
100100
</div>
101101
<?php endforeach; ?>
102102
</div>
103-
<p class='wp-block-newspack-blocks-donate__thanks'>
103+
<p class='wp-block-newspack-blocks-donate__thanks thanks'>
104104
<?php echo esc_html__( 'Your contribution is appreciated.', 'newspack-blocks' ); ?>
105105
</p>
106106
<button type='submit'>
@@ -120,10 +120,10 @@ class='donate-label'
120120
<form>
121121
<input type='hidden' name='newspack_donate' value='1' />
122122
<div class='wp-block-newspack-blocks-donate__options'>
123-
<div class='wp-block-newspack-blocks-donate__frequencies'>
123+
<div class='wp-block-newspack-blocks-donate__frequencies frequencies'>
124124
<?php foreach ( $frequencies as $frequency_slug => $frequency_name ) : ?>
125125

126-
<div class='wp-block-newspack-blocks-donate__frequency'>
126+
<div class='wp-block-newspack-blocks-donate__frequency frequency'>
127127
<input
128128
type='radio'
129129
value='<?php echo esc_attr( $frequency_slug ); ?>'
@@ -133,12 +133,12 @@ class='donate-label'
133133
/>
134134
<label
135135
for='newspack-donate-<?php echo esc_attr( $frequency_slug . '-' . $uid ); ?>'
136-
class='donation-frequency-label'
136+
class='donation-frequency-label freq-label'
137137
>
138138
<?php echo esc_html( $frequency_name ); ?>
139139
</label>
140140

141-
<div class='wp-block-newspack-blocks-donate__tiers'>
141+
<div class='wp-block-newspack-blocks-donate__tiers tiers'>
142142
<?php foreach ( $suggested_amounts as $index => $suggested_amount ) : ?>
143143
<div class='wp-block-newspack-blocks-donate__tier'>
144144
<?php
@@ -153,7 +153,7 @@ class='donation-frequency-label'
153153
<?php checked( 1, $index ); ?>
154154
/>
155155
<label
156-
class='tier-select-label'
156+
class='tier-select-label tier-label'
157157
for='newspack-tier-<?php echo esc_attr( $frequency_slug . '-' . $uid ); ?>-<?php echo (int) $index; ?>'
158158
>
159159
<?php echo esc_html( $formatted_amount ); ?>
@@ -171,18 +171,18 @@ class='other-input'
171171
id='newspack-tier-<?php echo esc_attr( $frequency_slug . '-' . $uid ); ?>-other'
172172
/>
173173
<label
174-
class='tier-select-label'
174+
class='tier-select-label tier-label'
175175
for='newspack-tier-<?php echo esc_attr( $frequency_slug . '-' . $uid ); ?>-other'
176176
>
177177
<?php echo esc_html__( 'Other', 'newspack-blocks' ); ?>
178178
</label>
179179
<label
180-
class='other-donate-label'
180+
class='other-donate-label odl'
181181
for='newspack-tier-<?php echo esc_attr( $frequency_slug . '-' . $uid ); ?>-other-input'
182182
>
183183
<?php echo esc_html__( 'Donation amount', 'newspack-blocks' ); ?>
184184
</label>
185-
<div class='wp-block-newspack-blocks-donate__money-input'>
185+
<div class='wp-block-newspack-blocks-donate__money-input money-input'>
186186
<span class='currency'>
187187
<?php echo esc_html( $settings['currencySymbol'] ); ?>
188188
</span>
@@ -200,7 +200,7 @@ class='other-donate-label'
200200
<?php endforeach; ?>
201201
</div>
202202
</div>
203-
<p class='wp-block-newspack-blocks-donate__thanks'>
203+
<p class='wp-block-newspack-blocks-donate__thanks thanks'>
204204
<?php echo esc_html__( 'Your contribution is appreciated.', 'newspack-blocks' ); ?>
205205
</p>
206206
<button type='submit'>

0 commit comments

Comments
 (0)