/** * fire on uninstall. delete options. * * @static */ public static function uninstall_hook() { foreach (CPTP_Util::get_post_types() as $post_type) { delete_option($post_type . '_structure'); } delete_option('no_taxonomy_structure'); }
public function setting_structure_callback_function($option) { $post_type = str_replace('_structure', '', $option); $pt_object = get_post_type_object($post_type); $slug = $pt_object->rewrite['slug']; $with_front = $pt_object->rewrite['with_front']; $value = CPTP_Util::get_permalink_structure($post_type); $disabled = false; if (isset($pt_object->cptp_permalink_structure) and $pt_object->cptp_permalink_structure) { $disabled = true; } if (!$value) { $value = CPTP_DEFAULT_PERMALINK; } global $wp_rewrite; $front = substr($wp_rewrite->front, 1); if ($front and $with_front) { $slug = $front . $slug; } echo '<p><code>' . home_url() . '/' . esc_html($slug) . '</code> <input name="' . esc_attr($option) . '" id="' . esc_attr($option) . '" type="text" class="regular-text code ' . $this->disabled_string($disabled) . '" value="' . $value . '" ' . $this->disabled_string($disabled) . ' /></p>'; echo '<p>has_archive: <code>'; echo $pt_object->has_archive ? 'true' : 'false'; echo '</code> / '; echo 'with_front: <code>'; echo $pt_object->rewrite['with_front'] ? 'true' : 'false'; echo '</code></p>'; }
/** * * Add hook flush_rules * @since 0.7.9 * */ public function update_rules() { $post_types = CPTP_Util::get_post_types(); foreach ($post_types as $post_type) { add_action('update_option_' . $post_type . '_structure', array(__CLASS__, 'queue_flush_rules'), 10, 2); } add_action('update_option_no_taxonomy_structure', array(__CLASS__, 'queue_flush_rules'), 10, 2); }
public function test_get_date_front() { $post_type = rand_str(12); register_post_type($post_type, array("public" => true)); update_option($post_type . "_structure", "/%year%/%monthnum%/%day%/%post_id%/"); $this->assertEquals(CPTP_Util::get_date_front($post_type), ""); update_option($post_type . "_structure", "/%post_id%/"); $this->assertEquals(CPTP_Util::get_date_front($post_type), "/date"); }
/** * * Add hook flush_rules * @since 0.7.9 * */ public function update_rules() { $post_types = CPTP_Util::get_post_types(); $type_count = count($post_types); $i = 0; foreach ($post_types as $post_type) { add_action('update_option_' . $post_type . '_structure', array($this, 'queue_flush_rules'), 10, 2); } add_action('update_option_no_taxonomy_structure', array($this, 'queue_flush_rules'), 10, 2); }
public function setting_structure_callback_function($option) { $post_type = $option['post_type']; $name = $option['label_for']; $pt_object = get_post_type_object($post_type); $slug = $pt_object->rewrite['slug']; $with_front = $pt_object->rewrite['with_front']; $value = CPTP_Util::get_permalink_structure($post_type); $disabled = false; if (isset($pt_object->cptp_permalink_structure) and $pt_object->cptp_permalink_structure) { $disabled = true; } if (!$value) { $value = CPTP_DEFAULT_PERMALINK; } global $wp_rewrite; $front = substr($wp_rewrite->front, 1); if ($front and $with_front) { $slug = $front . $slug; } ?> <p> <code><?php echo home_url() . '/' . esc_html($slug); ?> </code> <input name="<?php echo esc_attr($name); ?> " id="<?php echo esc_attr($name); ?> " type="text" class="regular-text code " value="<?php echo esc_attr($value); ?> " <?php disabled($disabled, true, true); ?> /> </p> <p>has_archive: <code><?php echo $pt_object->has_archive ? 'true' : 'false'; ?> </code> / with_front: <code><?php echo $pt_object->rewrite['with_front'] ? 'true' : 'false'; ?> </code></p> <?php }
/** * * Setting Init * @since 0.7 * */ public function settings_api_init() { add_settings_section('cptp_setting_section', __("Permalink Setting for custom post type", 'cptp'), array($this, 'setting_section_callback_function'), 'permalink'); $post_types = CPTP_Util::get_post_types(); foreach ($post_types as $post_type) { if (isset($_POST['submit']) and isset($_POST['_wp_http_referer'])) { if (strpos($_POST['_wp_http_referer'], 'options-permalink.php') !== FALSE) { $structure = trim(esc_attr($_POST[$post_type . '_structure'])); #get setting #default permalink structure if (!$structure) { $structure = CPTP_DEFAULT_PERMALINK; } $structure = str_replace('//', '/', '/' . $structure); # first "/" #last "/" $lastString = substr(trim(esc_attr($_POST['permalink_structure'])), -1); $structure = rtrim($structure, '/'); if ($lastString == '/') { $structure = $structure . '/'; } update_option($post_type . '_structure', $structure); } } add_settings_field($post_type . '_structure', $post_type, array($this, 'setting_structure_callback_function'), 'permalink', 'cptp_setting_section', $post_type . '_structure'); register_setting('permalink', $post_type . '_structure'); } add_settings_field('no_taxonomy_structure', __("Use custom permalink of custom taxonomy archive.", 'cptp'), array($this, 'setting_no_tax_structure_callback_function'), 'permalink', 'cptp_setting_section'); register_setting('permalink', 'no_taxonomy_structure'); if (isset($_POST['submit']) && isset($_POST['_wp_http_referer']) && strpos($_POST['_wp_http_referer'], 'options-permalink.php') !== false) { if (!isset($_POST['no_taxonomy_structure'])) { $set = true; } else { $set = false; } update_option('no_taxonomy_structure', $set); } }
/** * * Get Custom Taxonomies parents. * @version 1.0 * */ public static function get_taxonomy_parents($id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array()) { $chain = ''; $parent = get_term($id, $taxonomy); if (is_wp_error($parent)) { return $parent; } if ($nicename) { $name = $parent->slug; } else { $name = $parent->name; } if ($parent->parent && $parent->parent != $parent->term_id && !in_array($parent->parent, $visited)) { $visited[] = $parent->parent; $chain .= CPTP_Util::get_taxonomy_parents($parent->parent, $taxonomy, $link, $separator, $nicename, $visited); } if ($link) { $chain .= '<a href="' . get_term_link($parent->term_id, $taxonomy) . '" title="' . esc_attr(sprintf(__("View all posts in %s"), $parent->name)) . '">' . $name . '</a>' . $separator; } else { $chain .= $name . $separator; } return $chain; }
/** * * Fix taxonomy = parent/child => taxonomy => child * @since 0.9.3 * * @param WP $obj */ public function parse_request($obj) { $taxes = CPTP_Util::get_taxonomies(); foreach ($taxes as $key => $tax) { if (isset($obj->query_vars[$tax])) { if (false !== strpos($obj->query_vars[$tax], '/')) { $query_vars = explode('/', $obj->query_vars[$tax]); if (is_array($query_vars)) { $obj->query_vars[$tax] = array_pop($query_vars); } } } } }
/** * * Fix taxonomy link outputs. * @since 0.6 * @version 1.0 * * @param string $termlink * @param Object $term * @param Object $taxonomy * * @return string */ public function term_link($termlink, $term, $taxonomy) { /** @var WP_Rewrite $wp_rewrite */ global $wp_rewrite; if (!$wp_rewrite->permalink_structure) { return $termlink; } if (get_option('no_taxonomy_structure')) { return $termlink; } $taxonomy = get_taxonomy($taxonomy); if ($taxonomy->_builtin) { return $termlink; } if (empty($taxonomy)) { return $termlink; } $wp_home = rtrim(home_url(), '/'); if (in_array(get_post_type(), $taxonomy->object_type)) { $post_type = get_post_type(); } else { $post_type = $taxonomy->object_type[0]; } $post_type_obj = get_post_type_object($post_type); $slug = $post_type_obj->rewrite['slug']; $with_front = $post_type_obj->rewrite['with_front']; $front = substr($wp_rewrite->front, 1); $termlink = str_replace($front, '', $termlink); if ($with_front) { $slug = $front . $slug; } $termlink = str_replace($wp_home, $wp_home . '/' . $slug, $termlink); if (!$taxonomy->rewrite['hierarchical']) { $termlink = str_replace($term->slug . '/', CPTP_Util::get_taxonomy_parents($term->term_id, $taxonomy->name, false, '/', true), $termlink); } return $termlink; }
/** * * get_arcihves_link * @version 2.2 03/27/14 * */ public function get_archives_link($link) { global $wp_rewrite; if (!isset($this->get_archives_where_r['post_type'])) { return $link; } $c = isset($this->get_archives_where_r['taxonomy']) && is_array($this->get_archives_where_r['taxonomy']) ? $this->get_archives_where_r['taxonomy'] : ''; //[steve] $t = $this->get_archives_where_r['post_type']; $this->get_archives_where_r['post_type'] = isset($this->get_archives_where_r['post_type_slug']) ? $this->get_archives_where_r['post_type_slug'] : $t; // [steve] [*** bug fixing] if (isset($this->get_archives_where_r['post_type']) and 'postbypost' != $this->get_archives_where_r['type']) { $blog_url = rtrim(home_url(), '/'); //remove front $front = substr($wp_rewrite->front, 1); $link = str_replace($front, '', $link); $blog_url = preg_replace('/https?:\\/\\//', '', $blog_url); $ret_link = str_replace($blog_url, $blog_url . '/' . '%link_dir%', $link); $post_type = get_post_type_object($this->get_archives_where_r['post_type']); if (empty($c)) { // [steve] if (isset($post_type->rewrite['slug'])) { $link_dir = $post_type->rewrite['slug']; } else { $link_dir = $this->get_archives_where_r['post_type']; } } else { // [steve] $c['name'] = 'category' == $c['name'] && get_option('category_base') ? get_option('category_base') : $c['name']; $link_dir = $post_type->rewrite['slug'] . '/' . $c['name'] . '/' . $c['termslug']; } if (!strstr($link, '/date/')) { $link_dir = $link_dir . CPTP_Util::get_date_front($post_type); } if ($post_type->rewrite['with_front']) { $link_dir = $front . $link_dir; } $ret_link = str_replace('%link_dir%', $link_dir, $ret_link); } else { $ret_link = $link; } $this->get_archives_where_r['post_type'] = $t; // [steve] reverting post_type to previous value return $ret_link; }
/** * * Fix taxonomy = parent/child => taxonomy => child * @since 0.9.3 * */ public function parse_request($obj) { $taxes = CPTP_Util::get_taxonomies(); foreach ($taxes as $key => $tax) { if (isset($obj->query_vars[$tax])) { if (strpos($obj->query_vars[$tax], "/") !== false) { $obj->query_vars[$tax] = array_pop(explode("/", $obj->query_vars[$tax])); } } } }
/** * Get permalink structure front for date archive. * * @since 1.0.0 * @param string $post_type post type name. * @return string */ public static function get_date_front($post_type) { $structure = CPTP_Util::get_permalink_structure($post_type); $front = ''; preg_match_all('/%.+?%/', $structure, $tokens); $tok_index = 1; foreach ((array) $tokens[0] as $token) { if ('%post_id%' == $token && $tok_index <= 3) { $front = '/date'; break; } $tok_index++; } return $front; }
/** * * Fix taxonomy link outputs. * @since 0.6 * @version 1.0 * */ public function term_link($termlink, $term, $taxonomy) { if (get_option('no_taxonomy_structure')) { return $termlink; } $taxonomy = get_taxonomy($taxonomy); if ($taxonomy->_builtin) { return $termlink; } if (empty($taxonomy)) { return $termlink; } $wp_home = rtrim(home_url(), '/'); $post_type = $taxonomy->object_type[0]; $post_type_obj = get_post_type_object($post_type); $slug = $post_type_obj->rewrite['slug']; $with_front = $post_type_obj->rewrite['with_front']; //拡張子を削除。 $str = array_shift(explode(".", get_option("permalink_structure"))); $str = rtrim(preg_replace("/%[a-z_]*%/", "", $str), '/'); //remove with front $termlink = str_replace($str . "/", "/", $termlink); if ($with_front === false) { $str = ""; } $slug = $str . "/" . $slug; $termlink = str_replace($wp_home, $wp_home . $slug, $termlink); if (!$taxonomy->rewrite['hierarchical']) { $termlink = str_replace($term->slug . '/', CPTP_Util::get_taxonomy_parents($term->term_id, $taxonomy->name, false, '/', true), $termlink); } return $termlink; }
/** * * @test * @group rewrite * @runInSeparateProcess * @preserveGlobalState disabled * */ public function test_cpt_date_archive_with_date_front() { update_option($this->post_type . "_structure", "/%year%/%post_id%/"); register_post_type($this->post_type, array("public" => true, 'taxonomies' => array('category'), "has_archive" => true)); $post_type_object = get_post_type_object($this->post_type); $this->factory->post->create_many(10, array('post_type' => $this->post_type, "post_date" => "2012-12-12")); do_action('wp_loaded'); /** @var WP_Rewrite $wp_rewrite */ global $wp_rewrite; $wp_rewrite->flush_rules(); $this->go_to(home_url("/" . $post_type_object->rewrite["slug"] . CPTP_Util::get_date_front($this->post_type) . "/2012")); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_year"); $this->go_to(next_posts(0, false)); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_year", "is_paged"); $this->go_to(home_url("/" . $post_type_object->rewrite["slug"] . CPTP_Util::get_date_front($this->post_type) . "/2012/12")); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_month"); $this->go_to(next_posts(0, false)); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_month", "is_paged"); $this->go_to(home_url("/" . $post_type_object->rewrite["slug"] . CPTP_Util::get_date_front($this->post_type) . "/2012/12/12")); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_day"); $this->go_to(next_posts(0, false)); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_day", "is_paged"); }
public function setting_no_tax_structure_callback_function() { $no_taxonomy_structure = CPTP_Util::get_no_taxonomy_structure(); echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked(false, $no_taxonomy_structure, false) . ' /> '; $txt = __("If you check this, the custom taxonomy's permalinks will be <code>%s/post_type/taxonomy/term</code>.", 'custom-post-type-permalinks'); echo sprintf(wp_kses($txt, array('code' => array())), esc_html(home_url())); }