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); }
public function test_add_rewrite_rules_page() { global $wp_rewrite; $args = array('rewrite' => 'to'); $connection_factory = new O2O_Connection_Factory(); $connection_factory->register('page_to_flat', 'page', 'flat_post_type', $args); $o2o_rewrites = new O2O_Rewrites($connection_factory); $o2o_rewrites->add_rewrite_rules(); $rules = $wp_rewrite->rewrite_rules(); $required_rewrites = array('([^/]+?)/flat-posts/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?connection_name=page_to_flat&connected_name=$matches[1]&feed=$matches[2]&connection_dir=to', '([^/]+?)/flat-posts/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?connection_name=page_to_flat&connected_name=$matches[1]&feed=$matches[2]&connection_dir=to', '([^/]+?)/flat-posts/page/?([0-9]{1,})/?$' => 'index.php?connection_name=page_to_flat&connected_name=$matches[1]&paged=$matches[2]&connection_dir=to', '([^/]+?)/flat-posts/?$' => 'index.php?connection_name=page_to_flat&connected_name=$matches[1]&connection_dir=to'); foreach ($required_rewrites as $regex => $replace) { $this->assertArrayHasKey($regex, $rules); $this->assertEquals($rules[$regex], $replace); } }