/mtv/wp-plugin.php

https://github.com/mterenzio/wordpress-mtv · PHP · 169 lines · 87 code · 25 blank · 57 comment · 13 complexity · 4451a9fa8a27c7da2cc4c9da6e982009 MD5 · raw file

  1. <?php
  2. /**
  3. * @package MTV
  4. * @version 1.0
  5. */
  6. /*
  7. Plugin Name: Wordpress MTV
  8. Plugin URI: http://blog.apps.chicagotribune.com
  9. Description: A simple framework for building custom apps and features
  10. on top of wordpress
  11. Author: Ryan Mark, Ryan Nagle
  12. Version: 1.0
  13. */
  14. include 'mtv.php';
  15. use mtv\shortcuts;
  16. /**
  17. * Initialize the MTV framework
  18. **/
  19. # MTV comes with an App for WordPressy stuff
  20. mtv\register_app( 'wp',
  21. dirname(__FILE__) . DIRECTORY_SEPARATOR . 'wp' );
  22. /**
  23. * Register javascript libraries
  24. **/
  25. $js_runtime_settings = array(
  26. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  27. 'current_blog_id' => get_current_blog_id(),
  28. 'DEBUG' => false
  29. );
  30. if ( defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ) {
  31. wp_register_script('mtv',
  32. plugins_url('/mtv/devjs/mtv.js'),
  33. array('jquery'),
  34. MTV_VERSION);
  35. wp_register_script('mtv-all',
  36. '',
  37. array('mtv'),
  38. MTV_VERSION);
  39. $js_runtime_settings['DEBUG'] = true;
  40. } else {
  41. wp_register_script('mtv-all',
  42. plugins_url('/mtv/mtv.min.js'),
  43. array('jquery'),
  44. MTV_VERSION);
  45. }
  46. wp_localize_script('mtv-all', 'WordPress', $js_runtime_settings);
  47. unset($js_runtime_settings);
  48. /**
  49. * Use the URL resolver for ajax calls
  50. **/
  51. $handle_ajax = function() {
  52. // get the url patterns for the current theme
  53. if ( file_exists( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
  54. include get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php';
  55. else if ( file_exists( get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
  56. include get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php';
  57. else
  58. throw Exception("Can't find a urls.php file in your theme");
  59. // whatever is in the $apps global is what we're going to load
  60. global $apps;
  61. // run MTV
  62. mtv\run( array(
  63. 'url' => get_default( $_REQUEST, 'path', '' ),
  64. 'url_patterns' => $ajax_url_patterns,
  65. 'apps' => $apps ) );
  66. // That's all folks
  67. exit;
  68. };
  69. add_action('wp_ajax_mtv', $handle_ajax);
  70. add_action('wp_ajax_nopriv_mtv', $handle_ajax);
  71. /**
  72. * Request handling
  73. **/
  74. add_filter( 'query_vars', function( $vars ) {
  75. // Where we bless query vars so WP doesn't throw them out
  76. $vars[] = 'path';
  77. return $vars;
  78. } );
  79. add_action( 'init', function() {
  80. /**
  81. * Is our chosen theme an MTV theme?
  82. * If not, we don't want to hijack rewrite rules and template selection
  83. **/
  84. if ( ! file_exists( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) &&
  85. ! file_exists( get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
  86. return; // nope
  87. /**
  88. * generate_rewrite_rules
  89. * Run immediately after WordPress generates it's rewrite rules. We'll replace all
  90. * the rules with one of ours. Our rule will route all requests into our url resolver.
  91. * We set this to run first so plugins can still add their own rules.
  92. **/
  93. add_action( 'generate_rewrite_rules', function( $wp_rewrite ) {
  94. // setup our hijack rule
  95. $newrules = array();
  96. $newrules['(.*)'] = 'index.php?path=$matches[1]';
  97. // We're feeling adventurous, override wordpress' processing with ours
  98. $wp_rewrite->rules = $newrules;
  99. }, 1, 1);
  100. /**
  101. * redirect_canonical
  102. * Correct opinionated wordpress redirects
  103. **/
  104. add_filter('redirect_canonical', function($redirect_url, $requested_url) {
  105. # Don't add trailing slashes to files.
  106. # if $redirect_url ends in '/' then
  107. if ( substr($redirect_url, -1) ) {
  108. $ext = pathinfo($requested_url,PATHINFO_EXTENSION);
  109. # if $requested_url ends in '.xml' or '.html' etc. then
  110. if ( in_array($ext, array('xml', 'html')) )
  111. return false;
  112. }
  113. }, 10, 2);
  114. /**
  115. * Reroute the rest of the application through our url resolver
  116. **/
  117. add_action( 'template_redirect', function() {
  118. // Where we figure out which view to use on the front end
  119. global $wp_query;
  120. // check for the path queryvar. That means we're on!
  121. if ( !($wp_query->query_vars['path'] === NULL) ) { // will work for the root path
  122. // reset wp_query's is_whatever flags and posts
  123. shortcuts\reset_wp_query();
  124. // get the url patterns for the current theme
  125. if ( file_exists( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
  126. include get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php';
  127. else if ( file_exists( get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
  128. include get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php';
  129. else
  130. throw new Exception("Can't find a urls.php file in your theme");
  131. // whatever is in the $apps global is what we're going to load
  132. global $apps;
  133. // run MTV
  134. mtv\run( array(
  135. 'url' => $wp_query->query_vars['path'],
  136. 'url_patterns' => $url_patterns,
  137. 'apps' => $apps ) );
  138. // That's all folks
  139. exit;
  140. }
  141. });
  142. });