Skip to content

Commit

Permalink
See pewresearch/pewresearch-org@74f7258 from refs/heads/main
Browse files Browse the repository at this point in the history
  • Loading branch information
prcdevgitbot committed May 15, 2024
1 parent 0d7695b commit 1f5c219
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion includes/staff-bylines/class-staff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
90 changes: 90 additions & 0 deletions includes/taxonomies/class-languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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<post_id>\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;
}

}

0 comments on commit 1f5c219

Please sign in to comment.