diff --git a/php/cpts/actors/class-calculations.php b/php/cpts/actors/class-calculations.php index d3c916a3..929e3fa9 100644 --- a/php/cpts/actors/class-calculations.php +++ b/php/cpts/actors/class-calculations.php @@ -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 ); diff --git a/php/cpts/characters/class-calculations.php b/php/cpts/characters/class-calculations.php index eaf5814e..3594af81 100644 --- a/php/cpts/characters/class-calculations.php +++ b/php/cpts/characters/class-calculations.php @@ -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'] ); + } } /** @@ -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 ); } } diff --git a/php/cpts/shows/class-calculations.php b/php/cpts/shows/class-calculations.php index d5dbce62..da902105 100644 --- a/php/cpts/shows/class-calculations.php +++ b/php/cpts/shows/class-calculations.php @@ -6,6 +6,8 @@ namespace LWTV\CPTs\Shows; +use LWTV\CPTs\Characters; + class Calculations { /** @@ -93,20 +95,13 @@ 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 ) { @@ -114,12 +109,12 @@ public function count_queers( $post_id, $type = 'count' ) { // 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? @@ -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( @@ -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 ) { @@ -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 @@ -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 ] ); } } @@ -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 ); @@ -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; diff --git a/php/theme/class-actor-characters.php b/php/theme/class-actor-characters.php index 47450d77..8473315d 100644 --- a/php/theme/class-actor-characters.php +++ b/php/theme/class-actor-characters.php @@ -21,7 +21,7 @@ class Actor_Characters { public function make( $actor_id, $format ) { // Early Bail - $valid_data = array( 'all', 'dead' ); + $valid_data = array( 'all', 'dead', 'count' ); if ( ! in_array( $format, $valid_data, true ) ) { return; } @@ -38,11 +38,9 @@ public function make( $actor_id, $format ) { * @param string $actor ID Actor ID * @param string $format Type of Output * - * @return array All the characters by ID. + * @return array number of characters for that actor. */ - public function all( $actor_id, $format ) { - $format = $format; - + public function generate_list( $actor_id ) { // if there is a character shadow tax, we need to get the characters from there. $get_shadow_tax = \Shadow_Taxonomy\Core\get_the_posts( $actor_id, Characters::SHADOW_TAXONOMY, Characters::SLUG ); @@ -51,9 +49,42 @@ public function all( $actor_id, $format ) { } elseif ( taxonomy_exists( Characters::SHADOW_TAXONOMY ) ) { $characters = $this->get_characters_from_taxonomy( $actor_id ); } else { - $characters = $this->get_characters_from_post_meta( $actor_id ); + return 0; + // phpcs:ignore Squiz.PHP.CommentedOutCode.Found + // $characters = $this->get_characters_from_post_meta( $actor_id ); } + return $characters; + } + + /** + * Generate count of characters + * + * @param string $actor ID Actor ID + * @param string $format Type of Output + * + * @return array number of characters for that actor. + */ + public function count( $actor_id, $format = 'count' ) { + $format = $format; + + $characters = $this->generate_list( $actor_id ); + + return count( $characters ); + } + + /** + * Generate list of characters + * + * @param string $actor ID Actor ID + * @param string $format Type of Output + * + * @return array All the characters by ID. + */ + public function all( $actor_id, $format ) { + $format = $format; + + $characters = $this->generate_list( $actor_id ); $build_data = $this->build_character_info( $characters, $actor_id ); return $build_data; @@ -69,8 +100,10 @@ public function all( $actor_id, $format ) { public function get_characters_from_shadow_tax( $shadow_array ) { $characters = array(); - foreach ( $shadow_array as $shadow ) { - $characters[] = $shadow->ID; + foreach ( $shadow_array as $shadow => $item ) { + if ( isset( $item->ID ) && ! empty( $item->ID ) ) { + $characters[] = $item->ID; + } } return $characters; @@ -142,29 +175,43 @@ public function build_character_info( array $character_array, int $actor_id ) { // @TODO: There needs to be a way to invalidate this and re-run without a re-save. $characters = array(); + $dead = array(); // Rebuild the character array in format: foreach ( $character_array as $char_id ) { $actors_array = get_post_meta( $char_id, 'lezchars_actor', true ); + if ( ! in_array( (string) $actor_id, $actors_array, true ) ) { + $term_id = get_post_meta( $char_id, sanitize_key( 'shadow_' . Characters::SHADOW_TAXONOMY . '_term_id' ), true ); + wp_remove_object_terms( (int) $actor_id, (int) $term_id, Characters::SHADOW_TAXONOMY ); + } + if ( 'publish' === get_post_status( $char_id ) && isset( $actors_array ) && ! empty( $actors_array ) ) { foreach ( $actors_array as $char_actor ) { if ( (int) $char_actor === (int) $actor_id ) { $characters[ $char_id ] = array( - 'id' => $char_id, - 'title' => get_the_title( $char_id ), - 'url' => get_the_permalink( $char_id ), - 'content' => get_the_content( $char_id ), - 'shows' => get_post_meta( $char_id, 'lezchars_show_group', true ), + 'id' => $char_id, + 'title' => get_the_title( $char_id ), + 'url' => get_the_permalink( $char_id ), + 'shows' => get_post_meta( $char_id, 'lezchars_show_group', true ), ); - } else { - // If the character is not associated with the actor, remove the character taxonomy from the actor. - $term_id = get_post_meta( $char_id, sanitize_key( 'shadow_' . Characters::SHADOW_TAXONOMY . '_term_id' ), true ); - wp_remove_object_terms( $char_actor, (int) $term_id, Characters::SHADOW_TAXONOMY ); + + if ( has_term( 'dead', 'lez_cliches', $char_id ) ) { + $dead[ $char_id ] = array( + 'id' => $char_id, + 'title' => get_the_title( $char_id ), + 'url' => get_the_permalink( $char_id ), + ); + } } } } } + update_post_meta( $actor_id, 'lezactors_char_count', count( $characters ) ); + update_post_meta( $actor_id, 'lezactors_char_list', $characters ); + update_post_meta( $actor_id, 'lezactors_dead_count', count( $dead ) ); + update_post_meta( $actor_id, 'lezactors_dead_list', $dead ); + return $characters; } @@ -181,7 +228,7 @@ public function dead( $actor_id, $format ) { $dead = array(); // Get array of characters (by ID) - $character_array = $this->all( $actor_id, 'all' ); + $character_array = $this->generate_list( $actor_id ); if ( is_array( $character_array ) ) { foreach ( $character_array as $char_id ) { @@ -201,6 +248,8 @@ public function dead( $actor_id, $format ) { } } + update_post_meta( $actor_id, 'lezactors_dead_count', count( $dead ) ); + return $dead; } } diff --git a/php/theme/class-show-characters.php b/php/theme/class-show-characters.php index ce858ad5..1fd26eed 100644 --- a/php/theme/class-show-characters.php +++ b/php/theme/class-show-characters.php @@ -60,20 +60,50 @@ public function make( $post_id, $format, $role = '' ) { } elseif ( taxonomy_exists( Characters::SHADOW_TAXONOMY ) ) { $characters = $this->get_characters_from_taxonomy( $post_id ); } else { - $characters = $this->get_characters_from_post_meta( $post_id, $format ); + return array(); + // phpcs:ignore Squiz.PHP.CommentedOutCode.Found + // $characters = $this->get_characters_from_post_meta( $post_id ); } + $clean_characters = $this->clean_character_array( $characters, $post_id ); + if ( ! empty( $role ) ) { - $build_data = $this->build_character_data( $characters, $post_id, $role ); + $build_data = $this->build_character_data( $clean_characters, $post_id, $role ); } else { - $build_data = $this->build_character_list( $characters, $post_id, $format ); + $build_data = $this->build_character_list( $clean_characters, $post_id, $format ); } return $build_data; } + public function clean_character_array( $characters, $show_id ) { + foreach ( $characters as $char_id ) { + $shows_array = get_post_meta( $char_id, 'lezchars_show_group', true ); + + 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]; + } + $shows_array_simple[] = $char_show['show']; + } + + // If the show is not in the simple array for this character, remove the character. + $term_id = get_post_meta( $char_id, sanitize_key( 'shadow_' . Characters::SHADOW_TAXONOMY . '_term_id' ), true ); + if ( ! in_array( (string) $show_id, $shows_array_simple, true ) ) { + wp_remove_object_terms( (int) $show_id, (int) $term_id, Characters::SHADOW_TAXONOMY ); + unset( $characters[ $char_id ] ); + } else { + // Add the tax for the character to the show. + wp_add_object_terms( (int) $show_id, (int) $term_id, Characters::SHADOW_TAXONOMY ); + } + + return $characters; + } + } + /** - * Get Characters For Show + * Build Character Data * * Get all the characters for a show, based on role type and output in * a customized format for the show page. @@ -86,7 +116,7 @@ public function make( $post_id, $format, $role = '' ) { */ public function build_character_data( $characters, $show_id, $role = 'regular' ): mixed { // Valid Roles: - $valid_roles = array( 'regular', 'recurring', 'guest' ); + $valid_roles = array( 'regular', 'recurring', 'guest', 'all' ); // If this isn't a show page, or there are no valid roles, bail. if ( Shows::SLUG !== get_post_type( $show_id ) || ! in_array( $role, $valid_roles, true ) ) { @@ -102,29 +132,29 @@ public function build_character_data( $characters, $show_id, $role = 'regular' ) // If the character is in this show, AND a published character, // AND has this role ON THIS SHOW we will pass the following // data to the character template to determine what to display. - if ( 'publish' === get_post_status( $char_id ) && isset( $shows_array ) && ! empty( $shows_array ) ) { + if ( isset( $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]; } - // Because of show IDs having SIMILAR numbers, we need to be a little more flexible. - // We PROBABLY don't need this anymore, but it's here just in case. - - // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual - if ( $char_show['show'] == $show_id && $char_show['type'] === $role ) { - $display[ $char_id ] = array( - 'id' => $char_id, - 'title' => get_the_title( $char_id ), - 'url' => get_the_permalink( $char_id ), - 'content' => get_the_content( $char_id ), - 'shows' => $shows_array, - 'show_from' => $show_id, - 'role_from' => $role, - ); + if ( ! (int) $char_show['show'] === (int) $show_id ) { + continue; + } + + $shows_roles[ $char_show['show'] ] = $char_show['type']; + $shows_array_simple[] = $char_show['show']; + } + + if ( 'all' === $role ) { + foreach ( array( 'regular', 'recurring', 'guest' ) as $all_role ) { + if ( $all_role === $shows_roles[ $show_id ] ) { + $display[ $all_role ][] = $this->build_role_data( $char_id, $show_id, $shows_array, $all_role ); + } } + } else { + $display[ $char_id ] = $this->build_role_data( $char_id, $show_id, $shows_array, $shows_roles[ $show_id ] ); } } } @@ -132,6 +162,32 @@ public function build_character_data( $characters, $show_id, $role = 'regular' ) return $display; } + /** + * Build Role Data + * + * Get all the characters for a show, based on role type and output in + * a customized format for the show page. + * + * @param int $char_id Character ID + * @param int $show_id ID of the show + * @param array $shows_array Array of show IDs + * @param string $role Role of the characters to look for + * + * @return array of characters with custom data to output + */ + public function build_role_data( $char_id, $show_id, $shows_array, $role ) { + $display = array( + 'id' => $char_id, + 'title' => get_the_title( $char_id ), + 'url' => get_the_permalink( $char_id ), + 'shows' => $shows_array, + 'show_from' => $show_id, + 'role_from' => $role, + ); + + return $display; + } + /** * Generate list of characters for shows * @@ -217,10 +273,6 @@ public function build_character_list( $characters, $show_id, $output ) { ++$char_counts['txirl']; } } - } else { - // If the character is not associated with the show, remove the character taxonomy from the show. - $term_id = get_post_meta( $char_id, sanitize_key( 'shadow_' . Characters::SHADOW_TAXONOMY . '_term_id' ), true ); - wp_remove_object_terms( $char_show['show'], (int) $term_id, Characters::SHADOW_TAXONOMY ); } } } @@ -231,10 +283,13 @@ public function build_character_list( $characters, $show_id, $output ) { $new_characters = $characters; } + update_post_meta( $show_id, 'lezshows_dead_count', $char_counts['dead'] ); + update_post_meta( $show_id, 'lezshows_char_count', count( $new_characters ) ); + update_post_meta( $show_id, 'lezshows_char_list', $new_characters ); + switch ( $output ) { case 'dead': // Count of dead characters - update_post_meta( $show_id, 'lezshows_dead_count', $char_counts['dead'] ); $return = $char_counts['dead']; break; case 'none': @@ -255,12 +310,10 @@ public function build_character_list( $characters, $show_id, $output ) { break; case 'query': // Array of all characters by ID - update_post_meta( $show_id, 'lezshows_char_list', $new_characters ); $return = $new_characters; break; case 'count': // Count of all characters on the show - update_post_meta( $show_id, 'lezshows_char_count', count( $new_characters ) ); $return = count( $new_characters ); break; } @@ -311,7 +364,6 @@ public function get_characters_from_post_meta( $show_id ) { return $characters; } - /** * Get characters from the taxonomy * diff --git a/php/wp-cli/cli-shadow.php b/php/wp-cli/cli-shadow.php index e189d2bf..506e83fc 100644 --- a/php/wp-cli/cli-shadow.php +++ b/php/wp-cli/cli-shadow.php @@ -165,7 +165,7 @@ public function sync_characters_to_shows( $one_post ) { // Add the tax for the character to the show. if ( ! has_term( $shadow_character->term_id, $shadow_cpt, $each_show['show'] ) ) { - wp_set_object_terms( $each_show['show'], $shadow_character->term_id, $shadow_cpt, true ); + wp_add_object_terms( (int) $each_show['show'], (int) $shadow_character->term_id, $shadow_cpt ); } } } @@ -190,10 +190,7 @@ public function sync_characters_to_actors( $one_post ) { $actors = ( ! is_array( $actors ) ) ? array( $actors ) : $actors; foreach ( $actors as $actor ) { - // Add the tax for the character to the actor. - if ( ! has_term( $shadow_character->term_id, $shadow_cpt, $actor ) ) { - wp_add_object_terms( $actor, $shadow_character->term_id, $shadow_cpt, true ); - } + wp_add_object_terms( (int) $actor, (int) $shadow_character->term_id, $shadow_cpt ); } } }