public function test_filter_posts_clauses() { global $wpdb; $args = array('from' => array('sortable' => true)); $connection = new O2O_Mock_Connection('test', 'post', 'page', $args); $this->connection_factory->add($connection); $query_vars = array('o2o_query' => array('connection' => 'test', 'direction' => 'from', 'id' => 1), 'orderby' => 'test'); $query = new WP_Query(); $query->query_vars = $query_vars; $o2o_query = new O2O_Query($this->connection_factory); $o2o_query->_action_parse_query($query); //required for other filters to work properly $clauses = $o2o_query->_filter_posts_clauses(array(), $query); $this->assertInternalType('array', $clauses); $this->assertArrayHasKey('orderby', $clauses); $this->assertEquals(" find_in_set({$wpdb->posts}.ID, '1, 2') ASC", $clauses['orderby']); }
public function init() { $query = new O2O_Query($this->connection_factory); $query->init(); if (function_exists('wpcom_vip_enable_term_order_functionality')) { //ensure that the ability to sort terms is setup on WordPress.com VIP wpcom_vip_enable_term_order_functionality(); } if ($this->rewrites_enabled) { $rewrites = new O2O_Rewrites($this->connection_factory); $rewrites->init(); } if (is_admin()) { if (!class_exists('O2O_Admin')) { require_once dirname(__DIR__) . '/admin/admin.php'; } $admin = new O2O_Admin($this->connection_factory); $admin->init(); } //@todo, move the below to a better location //allow custom templates based on connection type add_filter('archive_template', function ($template) { global $wp_query; if (is_o2o_connection()) { $additional_templates = array(); if (($post_type = (array) get_query_var('post_type')) && count($post_type) == 1) { $additional_templates[] = "o2o-{$wp_query->o2o_connection}-{$wp_query->query_vars['o2o_query']['direction']}-{$post_type[0]}.php"; $additional_templates[] = "o2o-{$wp_query->o2o_connection}-{$post_type[0]}.php"; } $additional_templates[] = "o2o-{$wp_query->o2o_connection}.php"; if ($o2o_template = locate_template($additional_templates)) { return $o2o_template; } } return $template; }); //redirect canonical o2o based pages to canonical add_filter('template_redirect', function () { global $wp_query, $wpdb; if (is_404() && is_o2o_connection() && !get_queried_object_id()) { $o2o_query = $wp_query->query_vars['o2o_query']; if ($connection = O2O_Connection_Factory::Get_Connection($o2o_query['connection'])) { if (isset($o2o_query['post_name'])) { $post_name = $o2o_query['post_name']; $name_post_types = $o2o_query['direction'] == 'to' ? $connection->from() : $connection->to(); $post_name = rawurlencode(urldecode($post_name)); $post_name = str_replace('%2F', '/', $post_name); $post_name = str_replace('%20', ' ', $post_name); $post_name = array_pop(explode('/', trim($post_name, '/'))); $post_types = array_map('esc_sql', (array) $name_post_types); $post_types_in = "('" . implode(', ', $post_types) . "')"; $post_id = $wpdb->get_var($wpdb->prepare("SELECT post_id from {$wpdb->postmeta} PM JOIN {$wpdb->posts} P ON P.ID = PM.post_id " . "WHERE meta_key = '_wp_old_slug' AND meta_value = %s AND post_type in {$post_types_in} limit 1", $post_name)); if ($post_id) { if ($link = get_permalink($post_id)) { wp_redirect($link, 301); } } } } } }, 10, 2); }