Skip to content

Commit

Permalink
Merge pull request #451 from LezWatch/feature/improve-shadow
Browse files Browse the repository at this point in the history
Speed up Actor/Show Pages
  • Loading branch information
Ipstenu committed Mar 19, 2024
2 parents d35fedb + f290109 commit c416004
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 108 deletions.
6 changes: 6 additions & 0 deletions php/cpts/actors/class-calculations.php
Expand Up @@ -34,6 +34,12 @@ public function count( $post_id, $type = 'count' ) {

if ( is_array( $characters ) ) {
foreach ( $characters as $char_id => $char_details ) {

// If the character isn't published, skip it.
if ( get_post_status( $char_id ) !== 'publish' ) {
continue;
}

$actors_array = get_post_meta( $char_id, 'lezchars_actor', true );
$is_dead = has_term( 'dead', 'lez_cliches', $char_id );

Expand Down
91 changes: 69 additions & 22 deletions php/cpts/characters/class-calculations.php
Expand Up @@ -46,21 +46,49 @@ public function death( $post_id ) {
* @return void
*/
public function sync_shows( $post_id, $shadow_character ) {
$show_group = get_post_meta( $post_id, 'lezchars_show_group', true );
$show_group = get_post_meta( $post_id, 'lezchars_show_group', true );
$shows_array_simple = array();

if ( $show_group ) {
foreach ( $show_group as $each_show ) {
// Remove the Array.
if ( is_array( $each_show['show'] ) ) {
$each_show['show'] = $each_show['show'][0];
}
if ( ! $show_group ) {
return;
}

foreach ( $show_group as $char_show ) {
// Remove the Array if it's there.
if ( is_array( $char_show['show'] ) ) {
$char_show['show'] = $char_show['show'][0];
}
$shows_array_simple[] = $char_show['show'];
}

// Add the tax for the character to the show.
wp_set_object_terms( $each_show['show'], $shadow_character->term_id, Characters::SHADOW_TAXONOMY, true );
// Get all shows with this character.
$shadow_queery = lwtv_plugin()->queery_taxonomy( Shows::SLUG, Characters::SHADOW_TAXONOMY, 'term_id', $shadow_character->term_id );

Shows::do_the_math( $each_show['show'] );
if ( is_object( $shadow_queery ) ) {
if ( $shadow_queery->have_posts() ) {
while ( $shadow_queery->have_posts() ) {
$shadow_queery->the_post();
$show_id = get_the_ID();

// If the show has the taxonomy but the character doesn't have it in the array, remove the taxonomy.
if ( ! in_array( (string) $show_id, $shows_array_simple, true ) ) {
wp_remove_object_terms( (int) $show_id, (int) $shadow_character->term_id, Characters::SHADOW_TAXONOMY );
}
}
}
}

foreach ( $show_group as $each_show ) {
// Remove the Array.
if ( is_array( $each_show['show'] ) ) {
$each_show['show'] = $each_show['show'][0];
}

// Add the tax for the character to the show.
wp_add_object_terms( (int) $each_show['show'], (int) $shadow_character->term_id, Characters::SHADOW_TAXONOMY );

Shows::do_the_math( $each_show['show'] );
}
}

/**
Expand All @@ -73,35 +101,54 @@ public function sync_shows( $post_id, $shadow_character ) {
*/
public function sync_actors( $post_id, $shadow_character ) {
$actors = get_post_meta( $post_id, 'lezchars_actor', true );
if ( $actors ) {
$actors = ( ! is_array( $actors ) ) ? array( $actors ) : $actors;
if ( ! $actors ) {
return;
}

$actors = ( ! is_array( $actors ) ) ? array( $actors ) : $actors;

// Get all actors with this character taxonomy.
$shadow_queery = lwtv_plugin()->queery_taxonomy( Actors::SLUG, Characters::SHADOW_TAXONOMY, 'term_id', $shadow_character->term_id );

foreach ( $actors as $actor ) {
// Add the tax for the character to the actor.
wp_add_object_terms( $actor, $shadow_character->term_id, Characters::SHADOW_TAXONOMY, true );
if ( is_object( $shadow_queery ) ) {
if ( $shadow_queery->have_posts() ) {
while ( $shadow_queery->have_posts() ) {
$shadow_queery->the_post();
$actor_id = get_the_ID();

Actors::do_the_math( $actor );
// If the show has the taxonomy but the character doesn't have it in the array, remove the taxonomy.
if ( ! in_array( (string) $actor_id, $actors, true ) ) {
wp_remove_object_terms( (int) $actor_id, (int) $shadow_character->term_id, Characters::SHADOW_TAXONOMY );
}
}
}
}

foreach ( $actors as $actor ) {
// Add the tax for the character to the actor.
wp_add_object_terms( (int) $actor, (int) $shadow_character->term_id, Characters::SHADOW_TAXONOMY );

Actors::do_the_math( $actor );
}
}

/**
* Does the Math
* @param int $post_id Post ID of character
* @param int $character_id Post ID of character
* @return n/a
*/
public function do_the_math( $post_id ) {
public function do_the_math( $character_id ) {

// Calculate Death
self::death( $post_id );
self::death( $character_id );

// Get the shadow tax ID
$shadow_character = \Shadow_Taxonomy\Core\get_associated_term( $post_id, Characters::SHADOW_TAXONOMY );
$shadow_character = \Shadow_Taxonomy\Core\get_associated_term( $character_id, Characters::SHADOW_TAXONOMY );

// Update Show data
self::sync_shows( $post_id, $shadow_character );
self::sync_shows( $character_id, $shadow_character );

// Update Actor data
self::sync_actors( $post_id, $shadow_character );
self::sync_actors( $character_id, $shadow_character );
}
}
51 changes: 17 additions & 34 deletions php/cpts/shows/class-calculations.php
Expand Up @@ -6,6 +6,8 @@

namespace LWTV\CPTs\Shows;

use LWTV\CPTs\Characters;

class Calculations {

/**
Expand Down Expand Up @@ -93,33 +95,26 @@ public function count_queers( $post_id, $type = 'count' ) {
}

// before we do the math, let's see if we have any characters:
$raw_char_count = lwtv_plugin()->get_characters_list( $post_id, 'count' );
$char_count = count( get_post_meta( $post_id, 'lezshows_char_list', true ) );

// Empty raw? Return 0.
if ( empty( $raw_char_count ) ) {
return 0;
}

$raw_char_count = ( ! is_array( $raw_char_count ) ) ? array( $raw_char_count ) : $raw_char_count;
$char_count = count( $raw_char_count );

// No characters? It's a zero.
if ( 0 === $char_count ) {
if ( empty( $char_count ) ) {
return 0;
}
$char_roles = get_post_meta( $post_id, 'lezshows_char_roles', true );

// Here we need to break down the scores:
if ( 'score' === $type ) {
$char_score = 0;

// If the count for all characters is 0, we don't need to run this.
if ( 0 !== $char_count ) {
$chars_regular = lwtv_plugin()->get_chars_for_show( $post_id, 'count', 'regular' );
$chars_recurring = lwtv_plugin()->get_chars_for_show( $post_id, 'count', 'recurring' );
$chars_guest = lwtv_plugin()->get_chars_for_show( $post_id, 'count', 'guest' );
$chars_regular = $char_roles['regular'];
$chars_recurring = $char_roles['recurring'];
$chars_guest = $char_roles['guest'];

// Points: Regular = 5; Recurring = 2; Guests = 1
$char_score = ( count( $chars_regular ) * 5 ) + ( count( $chars_recurring ) * 2 ) + count( $chars_guest );
$char_score = ( $chars_regular * 5 ) + ( $chars_recurring * 2 ) + $chars_guest;

// TODO: Consider ratio-ing - if there are a lot of guests and no regulars, that's bad, but if your guests
// are closer in line to the number of regulars... Maybe 4 guests to 1 regular?
Expand Down Expand Up @@ -334,7 +329,7 @@ public function show_character_score( $post_id ) {
/**
* Calculate show character data.
*/
public function show_character_data( $post_id ) {
public function show_character_data( $show_id ) {

// What role each character has
$role_data = array(
Expand Down Expand Up @@ -362,26 +357,23 @@ public function show_character_data( $post_id ) {
}

// Get array of characters (by ID)
$characters = lwtv_plugin()->get_characters_list( $post_id, 'query' );
$new_characters = array();
$characters = lwtv_plugin()->get_characters_list( $show_id, 'query' );

if ( is_array( $characters ) ) {
foreach ( $characters as $char_id ) {
$shows_array = get_post_meta( $char_id, 'lezchars_show_group', true );

if ( '' !== $shows_array && 'publish' === get_post_status( $char_id ) ) {
if ( is_array( $shows_array ) && ! empty( $shows_array ) ) {
foreach ( $shows_array as $char_show ) {

// Remove the array if it's there.
if ( is_array( $char_show['show'] ) ) {
$char_show['show'] = $char_show['show'][0];
}

// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
if ( $char_show['show'] == $post_id ) {
if ( $char_show['show'] == $show_id ) {
// Bump the array for this role
++$role_data[ $char_show['type'] ];
$new_characters[] = $char_id;

// Now we'll sort gender and stuff...
foreach ( $valid_taxes as $title => $taxonomy ) {
Expand All @@ -399,10 +391,7 @@ public function show_character_data( $post_id ) {
}

// Update the roles scores
update_post_meta( $post_id, 'lezshows_char_roles', $role_data );

// Update the characters
update_post_meta( $post_id, 'lezshows_char_list', $new_characters );
update_post_meta( $show_id, 'lezshows_char_roles', $role_data );

/**
* Update the taxonomies
Expand All @@ -411,7 +400,7 @@ public function show_character_data( $post_id ) {
* - lezshows_char_romantic
*/
foreach ( $valid_taxes as $title => $taxonomy ) {
update_post_meta( $post_id, 'lezshows_char_' . $title, $tax_data[ $title ] );
update_post_meta( $show_id, 'lezshows_char_' . $title, $tax_data[ $title ] );
}
}

Expand All @@ -429,11 +418,8 @@ public function show_character_data( $post_id ) {
*/
public function do_the_math( $post_id ) {

// Delete the data.
$delete_array = array( 'lezshows_char_list', 'lezshows_char_count', 'lezshows_dead_list', 'lezshows_dead_count', 'lezshows_the_score' );
foreach ( $delete_array as $delete_meta ) {
delete_post_meta( $post_id, $delete_meta );
}
// Generate character data
self::show_character_data( $post_id );

// Get the ratings
$score_show_rating = self::show_score( $post_id );
Expand All @@ -442,9 +428,6 @@ public function do_the_math( $post_id ) {
$score_chars_alive = $score_chars_total['alive'];
$score_chars_score = $score_chars_total['score'];

// Generate character data
self::show_character_data( $post_id );

// Calculate the full score
$calculate = ( $score_show_rating + $score_show_tropes + $score_chars_alive + $score_chars_score ) / 4;

Expand Down

0 comments on commit c416004

Please sign in to comment.