Skip to content

Commit

Permalink
Correct some logic in strpos() calls in API endpoint definitions.
Browse files Browse the repository at this point in the history
In PHP, `strpos()` returns boolean `false` when the substring is not
found, so `0 < ...` will never evaluate to true. This is likely a mistaken
parallel from JavaScript, where similar methods return `-1`.

See #1167.
  • Loading branch information
boonebgorges committed Sep 14, 2023
1 parent 5370509 commit 122982b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Core/API/APIWithMetaEndpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function filter_wp_to_pf_in_terms( $data, $post, $request ) { // phpcs:ig
if ( isset( $links['https://api.w.org/term'] ) ) {
$data->remove_link( 'https://api.w.org/term' );
foreach ( $links['https://api.w.org/term'] as $key => $term_link ) {
if ( 0 <= strpos( $term_link['href'], 'wp/v2/folders' ) ) {
if ( false === strpos( $term_link['href'], 'wp/v2/folders' ) ) {
$term_link['href'] = str_replace( 'wp/v2/folders', 'pf/v1/folders', $term_link['href'] );
$links['https://api.w.org/term'][ $key ] = $term_link;
}
Expand All @@ -216,7 +216,7 @@ public function filter_wp_to_pf_in_terms( $data, $post, $request ) { // phpcs:ig
if ( isset( $links['https://api.w.org/post_type'] ) ) {
$data->remove_link( 'https://api.w.org/post_type' );
foreach ( $links['https://api.w.org/post_type'] as $key => $term_link ) {
if ( 0 <= strpos( $term_link['href'], 'wp/v2/feeds' ) ) {
if ( false === strpos( $term_link['href'], 'wp/v2/feeds' ) ) {
$term_link['href'] = str_replace( 'wp/v2/feeds', 'pf/v1/feeds', $term_link['href'] );
$links['https://api.w.org/post_type'][ $key ] = $term_link;
}
Expand All @@ -231,7 +231,7 @@ public function filter_wp_to_pf_in_terms( $data, $post, $request ) { // phpcs:ig
if ( isset( $links['https://api.w.org/items'] ) ) {
$data->remove_link( 'https://api.w.org/items' );
foreach ( $links['https://api.w.org/items'] as $key => $term_link ) {
if ( 0 <= strpos( $term_link['href'], 'wp/v2/folders' ) ) {
if ( false === strpos( $term_link['href'], 'wp/v2/folders' ) ) {
$term_link['href'] = str_replace( 'wp/v2/folders', 'pf/v1/folders', $term_link['href'] );
$links['https://api.w.org/items'][ $key ] = $term_link;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/API/PF_REST_Taxonomies_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function filter_taxonomies_links( $data, $post, $request ) {
if ( isset( $links['https://api.w.org/items'] ) ) {
$data->remove_link( 'https://api.w.org/items' );
foreach ( $links['https://api.w.org/items'] as $key => $term_link ) {
if ( 0 <= strpos( $term_link['href'], 'wp/v2/folders' ) ) {
if ( false === strpos( $term_link['href'], 'wp/v2/folders' ) ) {
$term_link['href'] = str_replace( 'wp/v2/folders', 'pf/v1/folders', $term_link['href'] );
$links['https://api.w.org/items'][ $key ] = $term_link;
}
Expand Down

0 comments on commit 122982b

Please sign in to comment.