get_rules(); if ( empty( $rules ) ) { return null; } // Sort rules by priority ascending (lower number = higher priority). usort( $rules, function ( $a, $b ) { $priority_a = isset( $a['priority'] ) ? (int) $a['priority'] : 100; $priority_b = isset( $b['priority'] ) ? (int) $b['priority'] : 100; return $priority_a - $priority_b; } ); foreach ( $rules as $rule ) { // Skip disabled rules. if ( isset( $rule['enabled'] ) && ! $rule['enabled'] ) { continue; } if ( $this->evaluate_rule( $rule, $post_id ) ) { return sanitize_text_field( $rule['schema_type'] ); } } return null; } /** * Get all active rules. * * Rules are stored in the plugin settings option under the * 'auto_apply_rules' key. If no custom rules exist, the default * rules are returned. * * @return array[] Array of rule definitions. Each rule contains: * - condition_type (string) One of: post_type, category, tag, * template, url_pattern, content_contains. * - condition_value (string) The value to match against. * - schema_type (string) The schema.org type to assign. * - priority (int) Evaluation order (lower = first). * - enabled (bool) Whether the rule is active. */ public function get_rules() { $settings = get_option( self::OPTION_KEY, array() ); if ( ! empty( $settings[ self::RULES_KEY ] ) && is_array( $settings[ self::RULES_KEY ] ) ) { return $settings[ self::RULES_KEY ]; } return $this->get_default_rules(); } /** * Evaluate whether a single rule matches a post. * * @param array $rule A rule definition (see get_rules() for structure). * @param int $post_id WordPress post ID. * @return bool True if the rule matches, false otherwise. */ public function evaluate_rule( $rule, $post_id ) { if ( empty( $rule['condition_type'] ) || ! isset( $rule['condition_value'] ) || empty( $rule['schema_type'] ) ) { return false; } $post = get_post( $post_id ); if ( ! $post ) { return false; } $condition_type = $rule['condition_type']; $condition_value = $rule['condition_value']; switch ( $condition_type ) { case 'post_type': return $post->post_type === $condition_value; case 'category': return $this->post_has_term( $post_id, $condition_value, 'category' ); case 'tag': return $this->post_has_term( $post_id, $condition_value, 'post_tag' ); case 'template': $template = get_page_template_slug( $post_id ); // Allow matching the template filename or an empty string for default. return $template === $condition_value; case 'url_pattern': $permalink = get_permalink( $post_id ); if ( ! $permalink ) { return false; } // Suppress errors from invalid regex. $match = @preg_match( $condition_value, $permalink ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged return ( 1 === $match ); case 'content_contains': $content = $post->post_content; if ( empty( $content ) || empty( $condition_value ) ) { return false; } return false !== stripos( $content, $condition_value ); default: /** * Filters the evaluation of custom rule condition types. * * Allows third-party code to add custom condition types to the * rule engine. * * @since 1.0.0 * * @param bool $matches Whether the rule matches. Default false. * @param array $rule The rule definition. * @param int $post_id The post ID being evaluated. * @param string $condition_type The condition type slug. * @param string $condition_value The condition value to match. */ return apply_filters( 'schema_ai_evaluate_rule', false, $rule, $post_id, $condition_type, $condition_value ); } } /** * Get the default rules shipped with the plugin. * * These provide sensible defaults for common WordPress post types and * content categories. * * @return array[] Default rule definitions. */ public function get_default_rules() { $rules = array( array( 'condition_type' => 'post_type', 'condition_value' => 'post', 'schema_type' => 'Article', 'priority' => 10, 'enabled' => true, ), array( 'condition_type' => 'post_type', 'condition_value' => 'page', 'schema_type' => 'WebPage', 'priority' => 10, 'enabled' => true, ), array( 'condition_type' => 'category', 'condition_value' => 'recipe', 'schema_type' => 'Recipe', 'priority' => 5, 'enabled' => true, ), array( 'condition_type' => 'category', 'condition_value' => 'event', 'schema_type' => 'Event', 'priority' => 5, 'enabled' => true, ), array( 'condition_type' => 'category', 'condition_value' => 'faq', 'schema_type' => 'FAQPage', 'priority' => 5, 'enabled' => true, ), ); // Add WooCommerce Product rule if WooCommerce is active. if ( class_exists( 'WooCommerce' ) || in_array( 'woocommerce/woocommerce.php', (array) get_option( 'active_plugins', array() ), true ) ) { $rules[] = array( 'condition_type' => 'post_type', 'condition_value' => 'product', 'schema_type' => 'Product', 'priority' => 5, 'enabled' => true, ); } /** * Filters the default auto-apply rules. * * @since 1.0.0 * * @param array $rules Default rule definitions. */ return apply_filters( 'schema_ai_default_rules', $rules ); } /** * Save rules to the plugin settings option. * * Validates and sanitizes each rule before saving. * * @param array[] $rules Array of rule definitions to save. * @return bool True on success, false on failure. */ public function save_rules( $rules ) { if ( ! is_array( $rules ) ) { return false; } $sanitized = array(); foreach ( $rules as $rule ) { if ( empty( $rule['condition_type'] ) || empty( $rule['schema_type'] ) ) { continue; } $sanitized[] = array( 'condition_type' => sanitize_text_field( $rule['condition_type'] ), 'condition_value' => sanitize_text_field( isset( $rule['condition_value'] ) ? $rule['condition_value'] : '' ), 'schema_type' => sanitize_text_field( $rule['schema_type'] ), 'priority' => isset( $rule['priority'] ) ? absint( $rule['priority'] ) : 10, 'enabled' => isset( $rule['enabled'] ) ? (bool) $rule['enabled'] : true, ); } $settings = get_option( self::OPTION_KEY, array() ); if ( ! is_array( $settings ) ) { $settings = array(); } $settings[ self::RULES_KEY ] = $sanitized; return update_option( self::OPTION_KEY, $settings ); } /** * Delete a rule by its index. * * @param int $index Zero-based index of the rule to remove. * @return bool True on success, false if the index does not exist. */ public function delete_rule( $index ) { $rules = $this->get_rules(); $index = absint( $index ); if ( ! isset( $rules[ $index ] ) ) { return false; } array_splice( $rules, $index, 1 ); return $this->save_rules( $rules ); } /** * Check whether a post has a term with the given slug in the given taxonomy. * * @param int $post_id WordPress post ID. * @param string $slug Term slug to look for. * @param string $taxonomy Taxonomy name. * @return bool True if the post has the term. */ private function post_has_term( $post_id, $slug, $taxonomy ) { $terms = get_the_terms( $post_id, $taxonomy ); if ( ! $terms || is_wp_error( $terms ) ) { return false; } $slugs = wp_list_pluck( $terms, 'slug' ); return in_array( strtolower( $slug ), array_map( 'strtolower', $slugs ), true ); } }