| @@ -927,14 +927,11 @@ | ||
| 927 | 927 | * @param string $content Post content. |
| 928 | 928 | * @return string |
| 929 | 929 | */ |
| 930 | 930 | public function filter_content_by_category( $content ) { |
| 931 | - // Skip admin, REST, and non-singular contexts. | |
| 932 | - if ( is_admin() || defined( 'REST_REQUEST' ) && REST_REQUEST ) { | |
| 931 | + if ( is_admin() ) { | |
| 933 | 932 | return $content; |
| 934 | 933 | } |
| 935 | - // On category/taxonomy archives, template_include already handles protection. | |
| 936 | - // Skip here to avoid showing escaped shortcode HTML in post excerpts. | |
| 937 | 934 | if ( is_category() || is_tax() ) { |
| 938 | 935 | return $content; |
| 939 | 936 | } |
| 940 | 937 | $post_id = get_the_id(); |
| @@ -940,26 +937,37 @@ | ||
| 940 | 937 | $post_id = get_the_id(); |
| 941 | 938 | if ( !$post_id ) { |
| 942 | 939 | return $content; |
| 943 | 940 | } |
| 944 | - // Skip if the post itself already has protection (PS_Public handles it). | |
| 941 | + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { | |
| 942 | + return $content; | |
| 943 | + } | |
| 944 | + $term_data = $this->get_active_category_lock( $post_id ); | |
| 945 | + if ( !$term_data ) { | |
| 946 | + return $content; | |
| 947 | + } | |
| 948 | + $shortcode = $this->build_shortcode_from_term( $term_data['term_id'], $content ); | |
| 949 | + return do_shortcode( $shortcode ); | |
| 950 | + } | |
| 951 | + | |
| 952 | + /** | |
| 953 | + * @param int $post_id Post ID. | |
| 954 | + * @return array|null | |
| 955 | + */ | |
| 956 | + public function get_active_category_lock( int $post_id ) { | |
| 945 | 957 | $post_protection = get_post_meta( $post_id, 'passster_activate_protection', true ); |
| 946 | 958 | if ( $post_protection ) { |
| 947 | - return $content; | |
| 959 | + return null; | |
| 948 | 960 | } |
| 949 | - // Check each hierarchical taxonomy for protected terms. | |
| 950 | 961 | $term_data = $this->get_protected_term_for_post( $post_id ); |
| 951 | 962 | if ( !$term_data ) { |
| 952 | - return $content; | |
| 963 | + return null; | |
| 953 | 964 | } |
| 954 | - // Build atts and validate. | |
| 955 | 965 | $atts = $this->build_atts_from_term( $term_data['term_id'] ); |
| 956 | 966 | if ( PS_Conditional::is_valid( $atts ) ) { |
| 957 | - return $content; | |
| 967 | + return null; | |
| 958 | 968 | } |
| 959 | - // Build shortcode and render form. | |
| 960 | - $shortcode = $this->build_shortcode_from_term( $term_data['term_id'], $content ); | |
| 961 | - return do_shortcode( $shortcode ); | |
| 969 | + return $term_data; | |
| 962 | 970 | } |
| 963 | 971 | |
| 964 | 972 | /** |
| 965 | 973 | * Protect category archive pages. |
| @@ -998,16 +1006,9 @@ | ||
| 998 | 1006 | if ( !$taxonomy_obj || !$taxonomy_obj->hierarchical || !$taxonomy_obj->public ) { |
| 999 | 1007 | return; |
| 1000 | 1008 | } |
| 1001 | 1009 | // Check the term itself and then its ancestors for protection. |
| 1002 | - $protected_term_id = null; | |
| 1003 | - $ids_to_check = array_merge( array($term->term_id), get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' ) ); | |
| 1004 | - foreach ( $ids_to_check as $check_id ) { | |
| 1005 | - if ( get_term_meta( $check_id, 'passster_activate_protection', true ) ) { | |
| 1006 | - $protected_term_id = $check_id; | |
| 1007 | - break; | |
| 1008 | - } | |
| 1009 | - } | |
| 1010 | + $protected_term_id = $this->get_active_protected_term_id( $term ); | |
| 1010 | 1011 | if ( !$protected_term_id ) { |
| 1011 | 1012 | return; |
| 1012 | 1013 | } |
| 1013 | 1014 | // Use the protected term (may be an ancestor) for all subsequent meta lookups. |
| @@ -1057,8 +1058,24 @@ | ||
| 1057 | 1058 | // ------------------------------------------------------------------------- |
| 1058 | 1059 | // Helper Methods |
| 1059 | 1060 | // ------------------------------------------------------------------------- |
| 1060 | 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 | + /** | |
| 1061 | 1078 | * Get the first protected term for a given post. |
| 1062 | 1079 | * |
| 1063 | 1080 | * @param int $post_id Post ID. |
| 1064 | 1081 | * @return array|null Array with 'term_id' and 'taxonomy', or null. |
| @@ -1071,17 +1088,14 @@ | ||
| 1071 | 1088 | continue; |
| 1072 | 1089 | } |
| 1073 | 1090 | foreach ( $terms as $term ) { |
| 1074 | 1091 | // Check the term itself, then walk up its ancestor chain. |
| 1075 | - $ids_to_check = array_merge( array($term->term_id), get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ) ); | |
| 1076 | - foreach ( $ids_to_check as $check_id ) { | |
| 1077 | - $activate = get_term_meta( $check_id, 'passster_activate_protection', true ); | |
| 1078 | - if ( $activate ) { | |
| 1079 | - return array( | |
| 1080 | - 'term_id' => $check_id, | |
| 1081 | - 'taxonomy' => $taxonomy, | |
| 1082 | - ); | |
| 1083 | - } | |
| 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 | + ); | |
| 1084 | 1098 | } |
| 1085 | 1099 | } |
| 1086 | 1100 | } |
| 1087 | 1101 | return null; |