Skip to content

Commit

Permalink
Merge pull request #199 from PRX/segment_redirect
Browse files Browse the repository at this point in the history
Add media url filter to detect segments
  • Loading branch information
brandonhundt authored Apr 16, 2024
2 parents 2236cba + 2295a5a commit 06b9efb
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions wp-content/plugins/tw-endpoint-helper/tw-endpoint-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ function _peh_get_object_by_wp_migrated_legacy_alias_db( $slug ) {
'name' => 'node',
'replace' => 'node/',
),
array(
'name' => 'file',
'replace' => 'file/',
),
array(
'name' => 'taxonomy',
'replace' => 'taxonomy/term/',
Expand All @@ -388,8 +392,9 @@ function _peh_get_object_by_wp_migrated_legacy_alias_db( $slug ) {
// Return only if something is found.
if ( $id && $type ) {

$wp_id = '';
$wp_type = '';
$wp_id = '';
$wp_type = '';
$wp_meta_key = 'nid';

// Convert type from node to post_type if needed.
switch ( $type ) {
Expand All @@ -402,6 +407,11 @@ function _peh_get_object_by_wp_migrated_legacy_alias_db( $slug ) {
$wp_type = 'post';
break;

case 'media':
$wp_type = 'segment';
$wp_meta_key = 'fid';
break;

default:
$wp_type = $type;
break;
Expand All @@ -410,15 +420,20 @@ function _peh_get_object_by_wp_migrated_legacy_alias_db( $slug ) {
// Get WP Posts by node id.
$s_args = array(
'post_type' => $wp_type,
'meta_key' => 'nid',
'meta_key' => $wp_meta_key,
'meta_value' => $id,
'fields' => 'ids',
'posts_per_page' => 1,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false
);

$posts = get_posts( $s_args );

if ( $posts && ! is_wp_error( $posts ) ) {

$wp_id = $posts[0]->ID;
$wp_id = $posts[0];
}

if ( $wp_id && $wp_type ) {
Expand All @@ -435,6 +450,7 @@ function _peh_get_object_by_wp_migrated_legacy_alias_db( $slug ) {
return false;
}


/**
* Get post by slug.
*
Expand All @@ -448,6 +464,9 @@ function _peh_get_object_by_slug( $slug, $extra_args = array() ) {
'name' => $slug,
'post_status' => 'publish',
'posts_per_page' => 1,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false
);

if ( $extra_args ) {
Expand Down

0 comments on commit 06b9efb

Please sign in to comment.