94 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace PluginNamespace\Front;
 | |
| 
 | |
| class Rewrites {
 | |
| 
 | |
|     /**
 | |
|      * Class constructor.
 | |
|      *
 | |
|      * @since 1.0.0
 | |
|      *
 | |
|      * @return void
 | |
|      */
 | |
|     public function __construct() {
 | |
|         add_action( 'init', array( $this, 'add_rewrite_rules' ), 5 );
 | |
| 
 | |
|         add_filter( 'query_vars', array( $this, 'custom_query_vars' ) );
 | |
|         add_filter( 'nmsp_plugin_name_rewrite_urls', array( $this, 'build_rewrite_urls' ) );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Generate new rewrite rules and flush the cached values.
 | |
|      *
 | |
|      * @since 1.0.0
 | |
|      *
 | |
|      * @return void
 | |
|      */
 | |
|     public function add_rewrite_rules(): void {
 | |
|         $filters = apply_filters( 'nmsp_plugin_name_rewrite_urls', array() );
 | |
| 
 | |
|         add_rewrite_tag( 'foo', '([0-9]+)' );
 | |
| 
 | |
|         if ( is_iterable( $filters ) && count( $filters ) > 0 ) {
 | |
|             foreach ( $filters as $item ) {
 | |
|                 if ( $item['slug'] ) {
 | |
|                     $slug = str_replace( home_url() . '/', '', get_permalink( $item['id'] ) );
 | |
|                     add_rewrite_rule( '^' . $slug . '([^/]*' . $item['var'] . ')/([^/]*)/?', 'index.php?page_id=' . $item['id'] . '&$matches[1]=$matches[2]', 'top' );
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             flush_rewrite_rules();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Build the array of post IDs that have a slug for the URL
 | |
|      * along with what custom query variable to use.
 | |
|      *
 | |
|      * @since 1.0.0
 | |
|      *
 | |
|      * @return array
 | |
|      */
 | |
|     public function build_rewrite_urls(): array {
 | |
|         $pages = array();
 | |
| 
 | |
|         $page_list = array(
 | |
|             'NMSP_PLUGIN_SLUG_PAGE_NAME' => array( 'foo' ),
 | |
|         );
 | |
| 
 | |
|         foreach ( $page_list as $key => $params ) {
 | |
|             $page_id = get_option( constant( $key ) );
 | |
|             $page = ( $page_id ) ? get_post( $page_id ) : false;
 | |
|             if ( ! empty( $page ) ) {
 | |
|                 foreach ( $params as $param ) {
 | |
|                     $pages[] = array(
 | |
|                         'id'   => $page_id,
 | |
|                         'slug' => $page->post_name,
 | |
|                         'var'  => $param,
 | |
|                     );
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         return $pages;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Append custom query variables to the list WordPress maintinas
 | |
|      * with each request.
 | |
|      *
 | |
|      * @since 1.0.0
 | |
|      *
 | |
|      * @param array $variable
 | |
|      *
 | |
|      * @return array
 | |
|      */
 | |
|     public function custom_query_vars( array $vars ): array {
 | |
|         $vars[] = 'foo';
 | |
|         return $vars;
 | |
|     }
 | |
| }
 | |
| 
 | |
| new Rewrites();
 |