Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haku: filtteripuulatauksen optimointi #80

Open
wants to merge 4 commits into
base: PO-331
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Perustuu projektiin [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
- PO-288: Teeman värit manifest tiedostoon ja meta tagiin
- PO-267: Näytetään kun haku on käynnissä

### Muutettu
- PO-351: Optimoitu redikseen menevää dataa

### Korjattu
- PO-293: Vinkkien järjestäminen

Expand Down
57 changes: 0 additions & 57 deletions content/themes/custom/functions/function-api_data_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,6 @@ function map_api_tags( &$repeater ) {
}
}

/**
* Modify parents array
*
* @param array $parents Parent list.
* @return array Modified $parents.
*/
function map_api_parents( array $parents ) {
// Remove programs from parents
$parents = array_filter( $parents, function( $parent ) {
return ! empty( $parent->type ) && $parent->type !== 'program';
});

// Add logo data to agegroups
$parents = array_map( function( $parent ) {
if ( $parent->type === 'agegroup' ) {
$parent->logo = get_images_by_guid( $parent->guid );
}
return $parent;
}, $parents );

return $parents;
}

// loads all children of a page in a tree format
function get_child_page_tree( $post_id, $helper, $grandchildren = true ) {

Expand Down Expand Up @@ -181,40 +158,6 @@ function pofapilink_shortcode( $atts ) {
}
add_shortcode( 'pofapilink', 'pofapilink_shortcode' );

/**
* Get api images by guid
*
* @param string $guid Api item guid.
* @return mixed Image object or null.
*/
function get_images_by_guid( $guid ) {
$cache_key = 'get_images_by_guid/' . $guid . '/';
$result = wp_cache_get( $cache_key );
if ( empty( $result ) ) {
$args = [
'posts_per_page' => 1,
'post_type' => 'page',
'post_status' => 'publish',
'meta_query' => [
[
'key' => 'api_guid',
'value' => $guid,
],
],
];
$pages = \DustPress\Query::get_acf_posts( $args );

if ( ! empty( $pages ) ) {
map_api_images( $pages[0]->fields['api_images'] );
$result = $pages[0]->fields['api_images'][0]['logo'];
}

wp_cache_set( $cache_key, $result, null, HOUR_IN_SECONDS );
}

return $result;
}

// Get hero args
function get_hero_args() {
$args = [
Expand Down
114 changes: 100 additions & 14 deletions content/themes/custom/models/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,35 @@ protected function do_pagination( array $posts, stdClass $params ) {
* Get single acf post by post id
*
* @param mixed $post_id Post id.
* @return \WP_Post
* @return \stdClass
*/
protected function get_single_acf_post( $post_id ) {
$cache_key = 'acfpost/' . $post_id;
$post = wp_cache_get( $cache_key );
if ( empty( $post ) ) {
$post = \DustPress\Query::get_acf_post( $post_id );
$post_obj = get_post( $post_id );

// Construct the actual used post object
$post = (object) [
'post_title' => $post_obj->post_title,
'post_type' => $post_obj->post_type,
'permalink' => get_permalink( $post_id ),
'post_excerpt' => ! empty( $post_obj->post_excerpt ) ? $post_obj->post_excerpt : $post_obj->post_content,
'fields' => [
'api_guid' => get_field( 'api_guid', $post_id ),
'api_type' => get_field( 'api_type', $post_id ),
'api_path' => get_field( 'api_path', $post_id ),
'api_images' => get_field( 'api_images', $post_id ),
'tags' => get_field( 'tags', $post_id ),
'pof_tip_parent' => get_field( 'pof_tip_parent', $post_id ),
],
];

wp_cache_set( $cache_key, $post, null, HOUR_IN_SECONDS );

// Save id for guid data retrieval
$guid_cache_key = 'get_id_by_guid/' . $post->fields['api_guid'] . '/' . get_locale();
wp_cache_set( $guid_cache_key, $post_id, null, HOUR_IN_SECONDS );
}

return $post;
Expand Down Expand Up @@ -352,25 +372,17 @@ protected function get_post_data( stdClass $result, stdClass $params ) {
if ( $post->post_type === 'pof_tip' ) {

// Get tip parent link & title
$tip_cache_key = 'tip_parent/' . $id;
$parent_post = wp_cache_get( $tip_cache_key );
if ( empty( $parent_post ) ) {
// Get post parent
$parent_id = get_post_meta( $id, 'pof_tip_parent', true );
$parent_post = $this->get_single_acf_post( $parent_id );

wp_cache_set( $tip_cache_key, $parent_post, null, HOUR_IN_SECONDS );
}
$parent_post = $this->get_single_acf_post( $post->fields['pof_tip_parent'] );

// Overwrite tip link and title with parents
$post->permalink = $parent_post->permalink . '#' . $post->post_name;
$post->permalink = $parent_post->permalink . '#' . $post->post_title;
$post->post_title = $parent_post->post_title;
$post->fields = $parent_post->fields;
$post->fields['api_type'] = 'pof_tip';
}

// Map item parents
$api_path = json_decode_pof( $post->fields['api_path'] ?? '[]' );
$api_path = json_decode_pof( $post->fields['api_path'] ?? '[]' ) ?? [];
if ( $post->post_type === 'pof_tip' ) {
// If this is a tip then add the task itself to the path as well
$api_path[] = [
Expand All @@ -383,7 +395,7 @@ protected function get_post_data( stdClass $result, stdClass $params ) {
],
];
}
$post->parents = map_api_parents( $api_path );
$post->parents = array_reduce( $api_path, [ $this, 'map_api_parents' ], [] );

// Decode images
map_api_images( $post->fields['api_images'] );
Expand All @@ -408,6 +420,80 @@ protected function get_post_data( stdClass $result, stdClass $params ) {
return $result->posts;
}

/**
* Set api item parents
* Should be used via array_reduce
*
* @param array $parents Complete array.
* @param array|\stdClass $parent Parent object.
* @return array Modified $parents.
*/
public function map_api_parents( array $parents, $parent ) {
$parent = (object) $parent;

// Remove programs from parents
if ( ! $parent->type !== 'program' ) {

// Add logo data to agegroup parent
if ( $parent->type === 'agegroup' ) {
$parent->logo = $this->get_images_by_guid( $parent->guid );
}

$parents[] = $parent;
}

return $parents;
}

/**
* Get api images by guid
*
* @param string $guid Api guid of post to get images of.
* @return mixed
*/
public function get_images_by_guid( string $guid ) {
$id = $this->get_id_by_guid( $guid );
$result = null;
if ( ! empty( $id ) ) {
$post = $this->get_single_acf_post( $id );
$result = $post->fields['api_images'];
map_api_images( $result );
}

return $result;
}

/**
* Get post id by guid
*
* @param string $guid Post api guid.
* @return mixed
*/
public function get_id_by_guid( string $guid ) {
$cache_key = 'get_id_by_guid/' . $guid . '/' . get_locale();
$result = wp_cache_get( $cache_key );
if ( $result === false ) {
$args = [
'posts_per_page' => 1,
'post_type' => 'page',
'post_status' => 'publish',
'fields' => 'ids',
'meta_query' => [
[
'key' => 'api_guid',
'value' => $guid,
],
],
];

$query = ( new WP_Query( $args ) )->posts[0] ?? null;

wp_cache_set( $cache_key, $result, null, HOUR_IN_SECONDS );
}

return $result;
}

/**
* Get api item term
*
Expand Down