Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Last active January 24, 2018 09:06
Show Gist options
  • Save MaximeCulea/24a905c4a357b82929d11f013812de5f to your computer and use it in GitHub Desktop.
Save MaximeCulea/24a905c4a357b82929d11f013812de5f to your computer and use it in GitHub Desktop.
Change on the fly the permastructure for a content.
<?php namespace BEA\CPT_Section;
/*
* /!\ Warnings /!\
* - Only working with https://github.com/BeAPI/hm-rewrite class which must be loaded
* - PHP5.6+
* - The custom "Post" class is loaded as model and used, so it would break your site if you don't delete the related code
* - There is a namesapce declared, you might not use it, so simply delete it
*/
use \BEA\Post_Customize\Models\Post;
class Rewrites {
/** Use the trait */
use Singleton;
protected $rewrite_struct = 'r/%s';
protected $rewrite_tag;
protected function init() {
// Init tag with cpt name
$this->rewrite_tag = sprintf( '%%%s%%', BEA_CPT_SECTION_NAME );
add_filter( 'post_link', array( $this, 'rewrite_permalink' ), 10, 3 );
add_action( 'init', array( $this, 'add_rewrites' ) );
add_filter( 'bea_create_site_filter', [ $this, 'create_site_filter' ] );
}
/**
* Rewrite post permalink to include the section relationship
*
* @param string $link
* @param \WP_Post $object
* @param string $leave_name
*
* @author Maxime CULEA
*
* @return mixed
*/
public function rewrite_permalink( $link, \WP_Post $object, $leave_name ) {
if ( wp_is_post_revision( $object ) ) {
return $link;
}
$bea_post = new Post( $object );
$section = $bea_post->p2p_get_section();
global $wp_rewrite;
/** Not connected to a section, delete rewriting tag without replacing it */
if ( ! is_a( $section, 'BEA\CPT_Section\Models\Section' ) || ! in_array( $this->rewrite_tag, $wp_rewrite->rewritecode ) ) {
return str_replace( $this->rewrite_tag . '/', '', $link );
}
$slug = $section->post_name;
/** Error on section, delete rewriting tag without replacing it */
if ( empty( $slug ) ) {
return str_replace( $this->rewrite_tag . '/', '', $link );
}
/**
* While connected to a section, replace rewriting tag with 'r/{section_slug}'
* Then add the #{post_id} to the end for animation facilities
*/
return sprintf(
'%s#%s',
str_replace(
$this->rewrite_tag,
sprintf( $this->rewrite_struct, $slug ),
$link
),
$bea_post->get_ID()
);
}
/**
* On init, add rewrite rule to match new structured url for posts with the wanted php template
* As we are almost rewriting same as pages, we redeclare page's rewrite after custom one
*
* @author Maxime CULEA
*
* @return void
*/
public function add_rewrites() {
// Custom rewrite : prefix/section-slug/post-slug
hm_add_rewrite_rule( array(
'regex' => '^'.sprintf( $this->rewrite_struct, '[^/]+' ) . '/([^/]+)/?$',
'query' => 'index.php?name=$matches[1]&post_type=post',
'template' => 'single.php',
'body_class_callback' => function ( $classes = array() ) {
$classes[] = 'section-post';
return $classes;
},
'disable_canonical' => true,
)
);
// Page rewrite
hm_add_rewrite_rule( array(
'regex' => '^([^/]+)/?$',
'query' => 'pagename=$matches[1]&post_type=page',
'template' => 'page.php',
'disable_canonical' => true,
)
);
}
/**
* Change default option (permastructure) for site creation
*
* @param $options
*
* @author Maxime CULEA
*
* @return mixed
*/
public function create_site_filter( $options ) {
$options['permalink_structure'] = apply_filters(
'BEA\CPT_Section\Sites\permalink_structure',
sprintf(
'/%s/%s',
$this->rewrite_tag,
'%postname%'
)
);
return $options;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment