/** * Get filtered condition groups * * @since 2.0 * @return array */ public static function get_conditions() { global $wpdb, $wp_query, $post; if (!$wp_query->query && !$post || is_admin() || post_password_required()) { return array(); } // Return cache if present if (self::$condition_cache) { return self::$condition_cache; } $context_data['WHERE'] = $context_data['JOIN'] = $context_data['EXCLUDE'] = array(); $context_data = apply_filters('wpca/modules/context-data', $context_data); // Check if there are any rules for this type of content if (empty($context_data['WHERE'])) { return array(); } $context_data['WHERE'][] = "posts.post_type = '" . self::TYPE_CONDITION_GROUP . "'"; $post_status = array(self::STATUS_PUBLISHED, self::STATUS_NEGATED); $context_data['WHERE'][] = "posts.post_status IN ('" . implode("','", $post_status) . "')"; //Syntax changed in MySQL 5.5 and MariaDB 10.0 (reports as version 5.5) $wpdb->query('SET' . (version_compare($wpdb->db_version(), '5.5', '>=') ? '' : ' OPTION') . ' SQL_BIG_SELECTS = 1'); $groups_in_context = $wpdb->get_results("SELECT posts.ID, posts.post_parent " . "FROM {$wpdb->posts} posts " . implode(' ', $context_data['JOIN']) . "\n\t\t\t\tWHERE\n\t\t\t\t" . implode(' AND ', $context_data['WHERE']) . "\n\t\t\t", OBJECT_K); $groups_negated = $wpdb->get_results($wpdb->prepare("SELECT p.ID, p.post_parent " . "FROM {$wpdb->posts} p " . "WHERE p.post_type = '%s' " . "AND p.post_status = '%s' ", self::TYPE_CONDITION_GROUP, self::STATUS_NEGATED), OBJECT_K); $valid = array(); //Force update of meta cache to prevent lazy loading update_meta_cache('post', array_keys($groups_in_context + $groups_negated)); //Exclude sidebars that have unrelated content in same group foreach ($groups_in_context as $key => $sidebar) { $valid[$sidebar->ID] = $sidebar->post_parent; //TODO: move to modules foreach ($context_data['EXCLUDE'] as $exclude) { //quick fix to check for taxonomies terms if ($exclude == 'taxonomy') { if ($wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->term_relationships} WHERE object_id = '{$sidebar->ID}'") > 0) { unset($valid[$sidebar->ID]); break; } } if (get_post_custom_values(self::PREFIX . $exclude, $sidebar->ID) !== null) { unset($valid[$sidebar->ID]); break; } } } $handled_already = array_flip($valid); foreach ($groups_negated as $sidebar) { if (isset($valid[$sidebar->ID])) { unset($valid[$sidebar->ID]); } else { $valid[$sidebar->ID] = $sidebar->post_parent; } if (isset($handled_already[$sidebar->post_parent])) { unset($valid[$sidebar->ID]); } $handled_already[$sidebar->post_parent] = 1; } return self::$condition_cache = $valid; }