| @@ -1006,16 +1006,9 @@ | ||
| 1006 | 1006 | if ( !$taxonomy_obj || !$taxonomy_obj->hierarchical || !$taxonomy_obj->public ) { |
| 1007 | 1007 | return; |
| 1008 | 1008 | } |
| 1009 | 1009 | // Check the term itself and then its ancestors for protection. |
| 1010 | - $protected_term_id = null; | |
| 1011 | - $ids_to_check = array_merge( array($term->term_id), get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' ) ); | |
| 1012 | - foreach ( $ids_to_check as $check_id ) { | |
| 1013 | - if ( get_term_meta( $check_id, 'passster_activate_protection', true ) ) { | |
| 1014 | - $protected_term_id = $check_id; | |
| 1015 | - break; | |
| 1016 | - } | |
| 1017 | - } | |
| 1010 | + $protected_term_id = $this->get_active_protected_term_id( $term ); | |
| 1018 | 1011 | if ( !$protected_term_id ) { |
| 1019 | 1012 | return; |
| 1020 | 1013 | } |
| 1021 | 1014 | // Use the protected term (may be an ancestor) for all subsequent meta lookups. |
| @@ -1065,8 +1058,24 @@ | ||
| 1065 | 1058 | // ------------------------------------------------------------------------- |
| 1066 | 1059 | // Helper Methods |
| 1067 | 1060 | // ------------------------------------------------------------------------- |
| 1068 | 1061 | /** |
| 1062 | + * Check a term and its ancestors for active protection. | |
| 1063 | + * | |
| 1064 | + * @param \WP_Term $term Term to check (and walk up from). | |
| 1065 | + * @return int|null The protected term ID (itself or an ancestor), or null if none. | |
| 1066 | + */ | |
| 1067 | + public function get_active_protected_term_id( \WP_Term $term ) { | |
| 1068 | + $ids_to_check = array_merge( array($term->term_id), get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' ) ); | |
| 1069 | + foreach ( $ids_to_check as $check_id ) { | |
| 1070 | + if ( get_term_meta( $check_id, 'passster_activate_protection', true ) ) { | |
| 1071 | + return $check_id; | |
| 1072 | + } | |
| 1073 | + } | |
| 1074 | + return null; | |
| 1075 | + } | |
| 1076 | + | |
| 1077 | + /** | |
| 1069 | 1078 | * Get the first protected term for a given post. |
| 1070 | 1079 | * |
| 1071 | 1080 | * @param int $post_id Post ID. |
| 1072 | 1081 | * @return array|null Array with 'term_id' and 'taxonomy', or null. |
| @@ -1079,17 +1088,14 @@ | ||
| 1079 | 1088 | continue; |
| 1080 | 1089 | } |
| 1081 | 1090 | foreach ( $terms as $term ) { |
| 1082 | 1091 | // Check the term itself, then walk up its ancestor chain. |
| 1083 | - $ids_to_check = array_merge( array($term->term_id), get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ) ); | |
| 1084 | - foreach ( $ids_to_check as $check_id ) { | |
| 1085 | - $activate = get_term_meta( $check_id, 'passster_activate_protection', true ); | |
| 1086 | - if ( $activate ) { | |
| 1087 | - return array( | |
| 1088 | - 'term_id' => $check_id, | |
| 1089 | - 'taxonomy' => $taxonomy, | |
| 1090 | - ); | |
| 1091 | - } | |
| 1092 | + $protected_term_id = $this->get_active_protected_term_id( $term ); | |
| 1093 | + if ( $protected_term_id ) { | |
| 1094 | + return array( | |
| 1095 | + 'term_id' => $protected_term_id, | |
| 1096 | + 'taxonomy' => $taxonomy, | |
| 1097 | + ); | |
| 1092 | 1098 | } |
| 1093 | 1099 | } |
| 1094 | 1100 | } |
| 1095 | 1101 | return null; |