From 604049210b4f62bcecb1b64ea6052589a3fac2ba Mon Sep 17 00:00:00 2001 From: Beverly Guillermo Date: Thu, 7 Jul 2011 13:03:02 -0400 Subject: [PATCH 1/2] Use new 3.1.x functions, instead of Request::instance use Request::current and Kohana_Exception class --- kohana-for-wordpress.php | 10 ++++++++-- kohana_bootstrap.php | 7 ++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/kohana-for-wordpress.php b/kohana-for-wordpress.php index 5289da1..ebb52dd 100644 --- a/kohana-for-wordpress.php +++ b/kohana-for-wordpress.php @@ -544,8 +544,14 @@ function kohana_page_request($kr) $kr = ($kr=='wp_kohana_default_request') ? '' : $kr ; try { - $req = Request::instance($kr); - $req = $req->execute(); + if (method_exists('Request', 'instance')) { + $req = Request::instance($kr); + $req = $req->execute(); + } if (method_exists('Request', 'current')) { + $req = Request::current($kr); + } else { + throw new Exception("Unable to get request"); + } } catch( Exception $e ) { diff --git a/kohana_bootstrap.php b/kohana_bootstrap.php index 52cc7dc..ebfe09b 100644 --- a/kohana_bootstrap.php +++ b/kohana_bootstrap.php @@ -26,7 +26,12 @@ * @see http://docs.kohanaphp.com/features/exceptions * @see http://php.net/set_exception_handler */ -set_exception_handler(array('Kohana', 'exception_handler')); +if (method_exists('Kohana_Exception', 'handler')) { + set_exception_handler(array('Kohana_Exception', 'handler')); +} else { + set_exception_handler(array('Kohana', 'exception_handler')); +} + /** * Enable Kohana error handling, converts all PHP errors to exceptions. From 9d377c5c8493ce49c2057feb65dcc3b677e6d0a1 Mon Sep 17 00:00:00 2001 From: Beverly Guillermo Date: Thu, 7 Jul 2011 17:47:46 -0400 Subject: [PATCH 2/2] Need to create a request factory then execute --- kohana-for-wordpress.php | 192 +++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 96 deletions(-) diff --git a/kohana-for-wordpress.php b/kohana-for-wordpress.php index ebb52dd..8f1701b 100644 --- a/kohana-for-wordpress.php +++ b/kohana-for-wordpress.php @@ -1,12 +1,12 @@ -$op_value ){ @@ -156,20 +156,20 @@ function kohana_deactivate() } /** * Function adds the Kohana options page to wordpress dashboard - * @return + * @return */ function kohana_register_admin_menu() { - add_options_page("Kohana", "Kohana", 1, "Kohana", "kohana_admin_menu"); + add_options_page("Kohana", "Kohana", 1, "Kohana", "kohana_admin_menu"); } /** * Function includes the Kohana options/admin page for diplay - * @return + * @return */ function kohana_admin_menu() { - include_once dirname(__FILE__) . '/admin_menu.php'; + include_once dirname(__FILE__) . '/admin_menu.php'; } /** @@ -181,7 +181,7 @@ function set_plugin_meta($links, $file) { if ($file == $plugin) { return array_merge( $links, - array( sprintf( '%s', + array( sprintf( '%s', 'Kohana', __('Settings') ) ) ); } @@ -191,7 +191,7 @@ function set_plugin_meta($links, $file) { /** * Function returns false if Kohana is not set up - * @return + * @return */ function should_kohana_run() { @@ -202,31 +202,31 @@ function should_kohana_run() // If main kohana class file is not found in system path return false if( ! is_file( get_option('kohana_system_path') . 'classes/kohana.php' ) ) return false; - + // If default route not set return false if( ! get_option('kohana_default_controller') OR ! get_option('kohana_default_action') ) return false; - // We should be good to go, return true. + // We should be good to go, return true. return true; } /** * Function inspects the request as determined by wordpress and determines * if Kohana needs to handle any part of this request. - * + * * Steps this method follows are: - * + * * - Determine if the request is for a valid wordpress page/post * - If no : Determine if the query string contains a request for a valid Kohana controller * - If yes : Determine if the wordpress page has a Kohana routing option - * + * * If a valid Kohana controller is found and there is no valid wordpress page being * called then the request is changed to the Kohana front loader. - * + * * Details of the Kohana request is added to the global $wp class. Eg: * $wp->kohana->request = 'welcome/index' - * + * * @return array $request * @param array $request */ @@ -234,23 +234,23 @@ function kohana_request_filter($request) { // if kohana isn't set up skip if( ! should_kohana_run() ) return $request; - + global $wp; global $wpdb; - + // Get the wordpress page_name of our kohana front loader - $wp->kohana->front_loader_slug = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE ID = " . get_option('kohana_front_loader') ); - + $wp->kohana->front_loader_slug = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE ID = " . get_option('kohana_front_loader') ); + // attempt to validate the request by looking for a post or page id $requested_post_id = kohana_validate_wp_request($request); //error_log( "Found the post/page id of $requested_post_id" ); - + // If request is not for a valid word press page. Look for valid kohana request if( ! $requested_post_id && get_option('kohana_process_all_uri') ){ //error_log( "No page found and process all uri is enabled. Examining uri for Kohana controller request" ); // Parse query string and look for kohana type requests - $kohana_request = kohana_parse_request(); - + $kohana_request = kohana_parse_request(); + if( $kohana_request ){ $wp->kohana->request = $kohana_request; $wp->kohana->placement = get_option('kohana_default_placement'); @@ -259,11 +259,11 @@ function kohana_request_filter($request) $request['page_id'] = get_option('kohana_front_loader'); } - // Request is for our wordpress kohana front loader + // Request is for our wordpress kohana front loader } else if( $requested_post_id == get_option('kohana_front_loader') ) { //error_log( "Request for Kohana front loader provided. Examine URI for Kohana controller request" ); $kohana_request = kohana_parse_request(); - error_log("Kohana request is $kohana_request"); + error_log("Kohana request is $kohana_request"); $wp->kohana->request = ( $kohana_request ) ? $kohana_request : 'wp_kohana_default_request'; $wp->kohana->placement = get_option('kohana_default_placement'); // Just because we found the front loader, wp may still think this is a 404 @@ -283,7 +283,7 @@ function kohana_request_filter($request) /** * Returns the post id if the request is for a valid wordpress page/post. * Returns false or 0 if the request is going to result in a wordpress 404. - * + * * @return post id * @param array $request */ @@ -296,11 +296,11 @@ function kohana_validate_wp_request( $request ) if( is_wp_homepage( 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] ) ){ // Check to see if the home page is a wordpress page or blog listings if( get_option('page_on_front') ) { - // return the ID of the page + // return the ID of the page return get_option('page_on_front'); } } - + //request contains a page id or a post id if( $request['page_id'] || $request['p'] ) { return ( $request['page_id'] ) ? $request['page_id'] : $request['p'] ; @@ -309,12 +309,12 @@ function kohana_validate_wp_request( $request ) if( $request['pagename'] || $request['name'] ) { $name = ($request['pagename']) ? $request['pagename'] : $request['name']; $has_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name'"); - return ( $has_id ) ? $has_id : 0 ; + return ( $has_id ) ? $has_id : 0 ; } - + // This could be a request for our front loader with a Kohana Controller URI appended $full_uri = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; - $wp_uri = substr( $full_uri, strlen( get_option('home').'/' ) ); + $wp_uri = substr( $full_uri, strlen( get_option('home').'/' ) ); if( $wp->kohana->front_loader_slug == substr( $wp_uri, 0, strlen($wp->kohana->front_loader_slug) ) ){ return get_option('kohana_front_loader'); } @@ -329,34 +329,34 @@ function kohana_validate_wp_request( $request ) function is_wp_homepage( $full_uri ) { // Check to see if the request ends in a trailing slash if( substr( $full_uri, -1 ) == '/' ){ - $full_uri = substr( $full_uri, 0, -1 ); + $full_uri = substr( $full_uri, 0, -1 ); } return ( $full_uri == get_option('home') ) ? true : false; } /** * Function parses the query string to determine if a kohana request is being made. - * - * Function first looks for values assigned to 'kr' in the query string + * + * Function first looks for values assigned to 'kr' in the query string * eg: example.com/index.php?kr=examples/pagination - * + * * If nothing is found in $_GET['kr'] function parses the _SERVER['REQUEST_URI'] and * looks for possible request in standard Kohana format * eg: example.com/examples/pagination - * + * * Function then checks to make sure that Kohana has a valid controller. If found the * Kohana request is returned if not a blank string is returned. - * + * * @return string $kr */ function kohana_parse_request() { global $wp; - + $kr = ''; - + if( $_GET['kr'] ) { - $kr = $_GET['kr']; + $kr = $_GET['kr']; } else { $kr = str_replace('?'.$_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']); if( get_option('kohana_base_url') != '/' ){ @@ -365,9 +365,9 @@ function kohana_parse_request() } // Remove index.php from our string $kr = str_replace('/index.php','',$kr); - + //error_log("Starting point Examining KR: $kr"); - + // Remove slash from front kr string if( substr($kr,0,1) == '/' ){ $kr = substr($kr,1); @@ -378,31 +378,31 @@ function kohana_parse_request() $kr = substr( $kr,0,-1 ); } //error_log("Removed starting slash Examining KR: $kr"); - + // check for presense of the kohana front loader slug if( $wp->kohana->front_loader_slug == substr( $kr, 0, strlen($wp->kohana->front_loader_slug) ) ){ $kr = substr($kr, strlen($wp->kohana->front_loader_slug.'/') ); } //error_log("Removed front loader slug Examining KR: $kr"); - + // Get the controller name. - if( strpos($kr,'/') ){ + if( strpos($kr,'/') ){ $k_controller = substr( $kr, 0, strpos($kr,'/') ); } else { $k_controller = $kr; } if( $k_controller && ! $kr ) $kr = 'index'; - + //error_log("Found Controller = $k_controller :: Examining: $kr"); // Check for the presence of a kohana controller for current request if( $kr && is_file( get_option('kohana_application_path') .'classes/controller/'.$k_controller.get_option('kohana_ext') ) ){ return $kr; } - + // Look for a defined route if( $kr ) { - try + try { if( Route::get($k_controller) ){ return $kr; @@ -412,13 +412,13 @@ function kohana_parse_request() // Do nothing on exception } } - - + + return ''; } /** - * Function provides a filter on the wordpress list of pages typically used to build + * Function provides a filter on the wordpress list of pages typically used to build * navigation in templates. Function will remove the Kohana front loader unless the option * to include this page is present. * @@ -429,7 +429,7 @@ function kohana_page_filter($pages) { // if we are in the dashboard skip this filter if( is_admin() ) return $pages; - + foreach($pages as $i=>$page){ if( $page->ID == get_option('kohana_front_loader') && ! get_option('kohana_front_loader_in_nav') ){ unset($pages[$i]); @@ -439,13 +439,13 @@ function kohana_page_filter($pages) } /** - * Function provides a filter on the main wp class. + * Function provides a filter on the main wp class. * This filter is called after wp has been completely loaded and created * but before any content is loaded. - * - * If a Kohana request was found when filtering the wp request then this is where we + * + * If a Kohana request was found when filtering the wp request then this is where we * create the first Kohana Request object via the kohana_request() function. - * + * * Output from the Kohana request is placed into the wp class so that it is available * when it comes time to display the combined wp and Kohana results. * @@ -455,8 +455,8 @@ function kohana_page_filter($pages) function kohana_wp_filter($wp) { // if kohana isn't set up skip - if( ! should_kohana_run() ) return $wp; - + if( ! should_kohana_run() ) return $wp; + if( $wp->kohana->request ) { $wp->kohana->content = kohana_page_request( $wp->kohana->request ); } @@ -465,7 +465,7 @@ function kohana_wp_filter($wp) /** * Function provides a filter on the wordpress content before being displayed - * + * * If content has been loaded from a Kohana controller this is where it is added * to or replaces the wordpress content. * @@ -476,7 +476,7 @@ function kohana_the_content_filter($content) { // if kohana isn't set up skip if( ! should_kohana_run() ) return $content; - + global $wp; if( $wp->kohana->content ) { switch( $wp->kohana->placement ){ @@ -491,8 +491,8 @@ function kohana_the_content_filter($content) break; } } - - + + // Look for any Kohana requests that are dropped directly into the content $tag = "/\[request(.*?)\\]/"; $matches = array(); @@ -502,7 +502,7 @@ function kohana_the_content_filter($content) $content = str_replace('[request '.trim($match).']', kohana_request( trim($match) ), $content); } } - + return $content; } @@ -510,7 +510,7 @@ function kohana_the_content_filter($content) /** * Function provides a filter on the title of the wordpress our post/page. * When necessary the wordpress title is replaced with the Kohana title. - * + * * NOTE: This function handles both wordpress filters 'the_title' and 'single_post_title' * * @param string $title @@ -520,7 +520,7 @@ function kohana_title_filter($title) { // if kohana isn't set up skip if( ! should_kohana_run() ) return $title; - + global $wp; global $post; if( $wp->kohana->title && $title == $post->post_title && $post->ID == get_option('kohana_front_loader') ){ @@ -540,15 +540,15 @@ function kohana_page_request($kr) { if( ! should_kohana_run() ) return ''; global $wp; - - $kr = ($kr=='wp_kohana_default_request') ? '' : $kr ; - + + $kr = ($kr=='wp_kohana_default_request') ? '' : $kr ; + try { if (method_exists('Request', 'instance')) { $req = Request::instance($kr); $req = $req->execute(); } if (method_exists('Request', 'current')) { - $req = Request::current($kr); + $req = Request::factory($kr)->execute(); } else { throw new Exception("Unable to get request"); } @@ -562,14 +562,14 @@ function kohana_page_request($kr) } throw $e; } - + if( $req->title ){ $wp->kohana->title = $req->title; } if( $req->extra_head ){ $wp->kohana->extra_head = $req->extra_head; } - return $req->response; + return $req->response; } /** @@ -599,7 +599,7 @@ function kohana( $kr ){ * * Currently by default a site running this plugin will use Wordpress' i10n * class and the wordpress __() method for language translation. - * + * * @param string $string * @param array $values * @return string @@ -620,8 +620,8 @@ function __k($string, array $values = NULL, $lang = 'en-us') * Comment out the method __() in wp-includes/i10n.php */ if( ! function_exists('__') ){ - - + + function __($string, $values = NULL, $lang = 'en-us') { if( ! is_array( $values ) ){ @@ -629,16 +629,16 @@ function __($string, $values = NULL, $lang = 'en-us') $values = array(); $values[] = $temp; } - + if ($lang !== I18n::$lang) { // The message and target languages are different // Get the translation for this message $string = I18n::get($string); } - + return empty($values) ? $string : strtr($string, $values); - } - - + } + + }