__( 'is', 'adpresso' ), 'is_not' => __( 'is not', 'adpresso' ), ]; } /** * Konfiguriert das Auswahlfeld für Page Templates im Admin UI. */ public function get_value_config( array $stored_node_data = [] ): array { if ( ! function_exists( 'get_page_templates' ) ) { require_once ABSPATH . 'wp-admin/includes/theme.php'; } $relevant_post_types = apply_filters( 'adpresso_condition_page_template_post_types', [ 'page', 'post' ] ); $all_options = []; $all_options[] = [ 'value' => 0, 'label' => __( '— Default Template —', 'adpresso' ), ]; foreach ( $relevant_post_types as $post_type ) { $templates = wp_get_theme()->get_page_templates( null, $post_type ); if ( ! empty( $templates ) ) { $prefix = count( $relevant_post_types ) > 1 ? get_post_type_object( $post_type )->labels->singular_name . ': ' : ''; foreach ( $templates as $template_name => $template_file ) { $all_options[] = [ 'value' => $template_file, 'label' => $prefix . $template_name, ]; } } } $current_values = isset( $stored_node_data['value'] ) ? (array) $stored_node_data['value'] : []; foreach ( $current_values as $template_file ) { if ( is_string( $template_file ) && ! isset( $all_options[ $template_file ] ) ) { $all_options[ $template_file ] = sprintf( '%s [%s]', basename( $template_file ), __( 'selected, possibly inactive', 'adpresso' ) ); } } return [ 'type' => 'token_select', 'multiple' => true, 'options' => $all_options, 'placeholder' => __( 'Select page templates...', 'adpresso' ), 'description' => __( 'Show ads based on selected page templates.', 'adpresso' ), ]; } /** * Formats the selected template filenames for displaying. */ public function format_value( mixed $raw ): string { if ( ! is_array( $raw ) ) { $raw = [ $raw ]; } $all_template_options = $this->get_value_config()['options']; $names = array_map( function( $template_file ) use ( $all_template_options ) { if ( ! is_string( $template_file ) ) { return 'Invalid Value'; } return $all_template_options[ $template_file ] ?? basename( $template_file ); }, $raw ); return implode( ', ', $names ); } /** * Evaluate the condition. * * @param string $operator 'is' oder 'is_not'. * @param mixed $value Die ausgewählten Template-Dateinamen (Array von Strings, kann '' enthalten). * @param Entity $entity Das Objekt (Ad/Group/Placement), für das geprüft wird. * * @return bool True, wenn die Bedingung erfüllt ist. */ public function evaluate( string $operator, mixed $value, Entity $entity ): bool { $context = Plugin::get()->context; if ( isset( $context['is_singular'] ) && ! $context['is_singular'] ) { $selected_template_files = (array) $value; if ( $operator === 'is' ) { return false; } if ( $operator === 'is_not' ) { return ! in_array( '', $selected_template_files, true ); } return false; // Fallback } $current_post_id = get_queried_object_id(); if ( $current_post_id <= 0 ) { return false; // Konnte Post ID nicht ermitteln } $current_template_file = get_page_template_slug( $current_post_id ); if ( $current_template_file === 'default' ) { $current_template_file = ''; } $selected_template_files = array_map( 'strval', (array) $value ); if ( empty( $selected_template_files ) ) { return true; // Keine Templates ausgewählt -> Bedingung schränkt nicht ein } $is_match = in_array( $current_template_file, $selected_template_files, true ); if ( $operator === 'is' ) { return $is_match; } if ( $operator === 'is_not' ) { return ! $is_match; } // Unbekannter Operator return false; } }