resolve( self::SLUG ); return; } $gap = get_transient( self::TRANSIENT_KEY ); if ( false === $gap || ! is_array( $gap ) ) { $gap = $this->scan_for_unavailable_pro_features(); set_transient( self::TRANSIENT_KEY, $gap, DAY_IN_SECONDS ); } if ( empty( $gap['condition_count'] ) && empty( $gap['placement_count'] ) ) { Notification_Center::get()->resolve( self::SLUG ); return; } NotificationManager::get()->register( self::SLUG, $this->build_message( $gap ), [ 'type' => 'warning', 'location' => 'wp_notice', 'context' => 'all', 'dismissible' => true, ] ); } /** * Scans every published Ad/Group/Placement for a condition type or * placement type that doesn't resolve in the currently-registered registries. * * @return array{condition_count: int, placement_count: int} */ private function scan_for_unavailable_pro_features(): array { $condition_registry = Condition_Registry::get(); $placement_registry = PlacementTypeRegistry::get(); $condition_count = 0; foreach ( [ Constants::POST_TYPE_AD, Constants::POST_TYPE_GROUP, Constants::POST_TYPE_PLACEMENT ] as $post_type ) { foreach ( $this->get_published_ids( $post_type ) as $id ) { $tree = get_post_meta( $id, Constants::META_PREFIX . 'conditions_tree', true ); if ( $this->tree_has_unresolvable_condition( $tree, $condition_registry ) ) { $condition_count ++; } } } $placement_count = 0; foreach ( $this->get_published_ids( Constants::POST_TYPE_PLACEMENT ) as $id ) { $type = get_post_meta( $id, Constants::META_PREFIX . 'placement_type', true ); if ( ! empty( $type ) && ! is_array( $type ) && ! $placement_registry->get_by_id( $type ) ) { $placement_count ++; } } return [ 'condition_count' => $condition_count, 'placement_count' => $placement_count, ]; } /** * @param string $post_type * * @return int[] */ private function get_published_ids( string $post_type ): array { return get_posts( [ 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', ] ); } /** * @param mixed $tree_json * @param Condition_Registry $registry * * @return bool */ private function tree_has_unresolvable_condition( mixed $tree_json, Condition_Registry $registry ): bool { if ( empty( $tree_json ) || ! is_string( $tree_json ) ) { return false; } $tree = json_decode( $tree_json, true ); if ( ! is_array( $tree ) || empty( $tree['groups'] ) || ! is_array( $tree['groups'] ) ) { return false; } foreach ( $tree['groups'] as $group ) { foreach ( $group['conditions'] ?? [] as $condition ) { $type = $condition['type'] ?? null; if ( $type && ! $registry->get_condition( $type ) ) { return true; } } } return false; } /** * @param array{condition_count: int, placement_count: int} $gap * * @return string */ private function build_message( array $gap ): string { $parts = []; if ( ! empty( $gap['condition_count'] ) ) { $parts[] = sprintf( /* translators: %d is the number of affected Ads/Groups/Placements */ _n( '%d item has a targeting condition that requires AdPresso Pro', '%d items have a targeting condition that requires AdPresso Pro', $gap['condition_count'], 'adpresso' ), $gap['condition_count'] ); } if ( ! empty( $gap['placement_count'] ) ) { $parts[] = sprintf( /* translators: %d is the number of affected Placements */ _n( '%d Placement uses a placement type that requires AdPresso Pro', '%d Placements use a placement type that requires AdPresso Pro', $gap['placement_count'], 'adpresso' ), $gap['placement_count'] ); } return sprintf( /* translators: %s is the list of affected item counts */ __( 'You have active Ads, Groups, or Placements with Pro-only configuration that AdPresso Pro is not currently active to enforce: %s. These are not being applied right now - e.g. a targeting condition like this is silently skipped rather than restricting anything.', 'adpresso' ), implode( __( ' and ', 'adpresso' ), $parts ) ); } }