Last active
October 23, 2016 08:36
-
-
Save ScreamingDev/0f3133057c4748fa9b08 to your computer and use it in GitHub Desktop.
Doing Magento-like-Controllers in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // make the url "/register/index" and "/register" available | |
| class Register_Controller extends Abstract_Controller { | |
| public function index_action() { | |
| wp_die( 'You can register here' ); | |
| } | |
| } | |
| new Register_Controller(); | |
| // make the url "/magic/index" and "/magic" available | |
| class Magic_Controller extends Abstract_Controller { | |
| public function index_action() { | |
| wp_die( 'Das ist ja wonderbra!' ); | |
| } | |
| } | |
| new Magic_Controller(); | |
| // !!! Do not forget to `flush_rewrite_rules()` !!! | |
| // magic upcoming ... | |
| abstract class Abstract_Controller { | |
| public function __construct() { | |
| $this->add_action( 'init', 10, 0 ); | |
| $this->add_action( 'parse_request', 0 ); | |
| $this->add_filter( 'rewrite_rules_array' ); | |
| } | |
| public function init() { | |
| add_rewrite_tag( '%rmp_controller%', '([^&]+)' ); | |
| add_rewrite_tag( '%rmp_action%', '([^&]+)' ); | |
| } | |
| public function parse_request() { | |
| /** @var WP $wp */ | |
| global $wp; | |
| // is it some kind of my controller? | |
| if ( ! isset( $wp->query_vars['rmp_controller'] ) ) { | |
| return; | |
| } | |
| // is it this controller? | |
| if ( get_class( $this ) != $wp->query_vars['rmp_controller'] ) { | |
| return; | |
| } | |
| if ( ! isset( $wp->query_vars['rmp_action'] ) ) { | |
| return; | |
| } | |
| $action = 'index_action'; | |
| if ( $wp->query_vars['rmp_action'] ) { | |
| $action = $wp->query_vars['rmp_action'] . '_action'; | |
| } | |
| // is there an action? | |
| if ( ! method_exists( $this, $action ) ) { | |
| $action = 'not_found_action'; | |
| } | |
| $this->$action(); | |
| } | |
| public function not_found_action() { | |
| global $wp_query; | |
| $wp_query->set_404(); | |
| status_header( 404 ); | |
| get_template_part( 404 ); | |
| exit(); | |
| } | |
| public function index_action() { | |
| wp_die( 'Yuck! Some developer forgot about this magic place.' ); | |
| } | |
| public function get_regexp() { | |
| $prefix = str_replace( '\\', '/', get_class( $this ) ); | |
| $prefix = trim( $prefix, '/' ); | |
| $prefix = preg_replace( '@(.*)_Controller$@', '$1', $prefix ); | |
| $prefix = strtolower( $prefix ); | |
| return $prefix . '/?([^/]*)/?'; | |
| } | |
| public function rewrite_rules_array( $rules ) { | |
| return array_merge( | |
| array( | |
| $this->get_regexp() => | |
| sprintf( | |
| 'index.php?rmp_controller=%s&rmp_action=$matches[1]', | |
| rawurldecode( get_class( $this ) ) | |
| ) | |
| ), | |
| $rules | |
| ); | |
| } | |
| protected function add_action( $tag, $priority = 10, $accepted_args = 1 ) { | |
| add_action( $tag, array( $this, $tag ), $priority, $accepted_args ); | |
| } | |
| protected function add_filter( $tag, $priority = 10, $accepted_args = 1 ) { | |
| add_filter( $tag, array( $this, $tag ), $priority, $accepted_args ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment