From 1f5c21964534f29e4dbb608963d1f1e35f2ba265 Mon Sep 17 00:00:00 2001 From: prcdevgitbot Date: Wed, 15 May 2024 17:56:32 +0000 Subject: [PATCH] See https://github.com/pewresearch/pewresearch-org/commit/74f7258a4c4f17a04cd3880b8a48b8dc4ac91367 from refs/heads/main --- includes/staff-bylines/class-staff.php | 2 +- includes/taxonomies/class-languages.php | 90 +++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/includes/staff-bylines/class-staff.php b/includes/staff-bylines/class-staff.php index 3f6da2ff..c0154919 100644 --- a/includes/staff-bylines/class-staff.php +++ b/includes/staff-bylines/class-staff.php @@ -204,7 +204,7 @@ public function get_expertise($staff_post_id = false) { public function get_staff_photo($staff_post_id = false) { $staff_photo_data = false; $staff_photo_id = get_post_thumbnail_id($staff_post_id); - $staff_photo = wp_get_attachment_image_src($staff_photo_id, 'large'); + $staff_photo = wp_get_attachment_image_src($staff_photo_id, 'full'); $staff_portrait = wp_get_attachment_image_src($staff_photo_id, '320-portrait'); if ( false !== $staff_photo || false !== $staff_portrait ) { $staff_photo_data = []; diff --git a/includes/taxonomies/class-languages.php b/includes/taxonomies/class-languages.php index 482b92fd..16509735 100644 --- a/includes/taxonomies/class-languages.php +++ b/includes/taxonomies/class-languages.php @@ -10,6 +10,7 @@ class Languages extends Taxonomies { public function __construct($loader) { $loader->add_action( 'init', $this, 'register' ); + $loader->add_filter( 'prc_api_endpoints', $this, 'register_endpoints' ); } public function register() { @@ -82,4 +83,93 @@ public function set_language_on_pub( $post ) { $this->set_language_from_content( $post->ID, $post->post_content ); } + /** + * @hook prc_api_endpoints + */ + public function register_endpoints($endpoints) { + array_push($endpoints, array( + 'route' => 'utils/translate/(?P\d+)', + 'methods' => 'GET', + 'callback' => array( $this, 'restfully_get_post_for_translation' ), + 'args' => array(), + 'permission_callback' => function () { + return true; + }, + )); + return $endpoints; + } + + protected function recursively_search_for_block_attrs_and_return($block, $attributes_to_return = []) { + $inner_blocks = array_key_exists('innerBlocks', $block) ? $block['innerBlocks'] : null; + $attributes = array_key_exists('attributes', $block) ? $block['attributes'] : null; + + // Return early if there are no attributes to parse. + if (empty($attributes)) { + return $block; + } + + // go through the attributes and remove any that arent in the $attributes_to_return array + foreach ($attributes as $key => $value) { + if (!in_array($key, $attributes_to_return)) { + unset($attributes[$key]); + } + // if the value is empty remove it as well + if (empty($value)) { + unset($attributes[$key]); + } + } + + // loop through the block's innerblocks and strip out the attributes that arent included in the $attributes_to_return array but keep everyything else intact + if ( !empty($inner_blocks) ) { + $i = 0; + foreach ($inner_blocks as $inner_block) { + $inner_blocks[$i] = $this->recursively_search_for_block_attrs_and_return($inner_block, $attributes_to_return); + $i++; + } + $block['innerBlocks'] = $inner_blocks; + } + + $block['attributes'] = $attributes; + + // for cleanliness, and because I'm sure we'll be charged for empty space, remove attribute if its empty in the end. + if (empty($block['attributes'])) { + unset($block['attributes']); + } + // we need to maintain at least the structure and block name to reimport so that has to remain even if the block isn't translated. + + return $block; + } + + public function restfully_get_post_for_translation( \WP_REST_Request $request ) { + $post_id = $request->get_param( 'post_id' ); + $blocks = $this->provide_object_for_translation($post_id); + return $blocks; + } + + public function provide_object_for_translation($post_id) { + $rest_endpoint = '/vip-block-data-api/v1/posts/'.$post_id.'/blocks'; + $attributes_to_return = [ + 'content', + 'alt', + 'caption', + 'label', + ]; + + // Build FacetWP rest request. + $request = new \WP_REST_Request( 'GET', $rest_endpoint ); + // Send request. + $response = rest_do_request( $request ); + $server = rest_get_server(); + $data = $server->response_to_data( $response, false ); + if ( !array_key_exists('blocks', $data) ) { + return []; + } + $blocks = $data['blocks']; + $tmp = []; + foreach ($blocks as $i => $block) { + $tmp[$i] = $this->recursively_search_for_block_attrs_and_return($block, $attributes_to_return); + } + return $tmp; + } + }