Skip to content

Instantly share code, notes, and snippets.

@gibrown
Created November 20, 2013 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gibrown/7568463 to your computer and use it in GitHub Desktop.
Save gibrown/7568463 to your computer and use it in GitHub Desktop.
<?php
/**
* VIPs need some special vetting as they can have custom code in the functions.php
* to setup custom taxonomies / post types and the like.
*
* We need to load the functions.php file for these themes.
*
* An other problem is that things which are usually attached to early hooks such as init or after_theme_setup
* in order to register custom taxonomies or post types are not working as the init hook already fired off when the jobs action is executed.
* So we copy all functions attached to the hooks we need ( currently after_theme_setup, init ) to an other hook and fire it off
*
* @todo: this is run from within a function. we might need to globalize vars
* @todo: be aware that this is a dirty workaround. it would be better if we initialize the whole environment. a possible solution would be to let jobs functions that need a full environment spawn a new process via system() call that would then initialize a new wp instance with the HTTP_HOST and URI, as well as other job parameters set from the beginning, avoiding any switch_to_blog calls and the like.
*/
if ( ( !defined( 'WPCOM_JOBS' ) || true <> WPCOM_JOBS ) && ( !defined( 'POST_BY_EMAIL' ) || true <> POST_BY_EMAIL ) )
return;
function copy_hooks( $from_hook, $to_hook, $base_path='' ) {
global $wp_filter;
foreach ( $wp_filter as $hook => $actions ) {
if ( $from_hook <> $hook )
continue;
foreach ( (array) $actions as $priority => $callbacks ) {
foreach( $callbacks as $callback_key => $callback_data ) {
$callback = $callback_data['function'];
$reflection = get_reflection( $callback ); // use reflection api to determine filename where function is defined
if ( false !== $reflection ) {
$file_name = $reflection->getFileName();
if ( 0 === strpos( $file_name, $base_path ) ) { // only copy hooks with functions which are defined in the theme context
$wp_filter[$to_hook][$priority]['cph' . $callback_key] = $callback_data;
}
}
}
}
}
}
function get_reflection( $callback ) {
if ( is_array( $callback ) ) {
list( $class, $method ) = $callback;
return new ReflectionMethod( $class, $method );
}
if ( is_string( $callback ) && strpos( $callback, "::" ) !== false ) {
list( $class, $method ) = explode( "::", $callback );
return new ReflectionMethod( $class, $method );
}
if ( version_compare( PHP_VERSION, "5.3.0", ">=" ) && method_exists( $callback, "__invoke" ) ) {
return new ReflectionMethod( $callback, "__invoke" );
}
if ( is_string( $callback ) && strpos( $callback, "::" ) == false && function_exists( $callback ) ) {
return new ReflectionFunction( $callback );
}
return false;
}
/*
* Disable kses()
*/
if ( function_exists( 'vip_kses' ) && is_super_admin() ) {
vip_kses();
vip_kses_import();
kses_remove_filters();
}
if ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ) {
require_once( ABSPATH . 'wp-content/themes/vip/plugins/vip-do-not-include-on-wpcom/vip-local-development-helper/vip-local-development-helper.php' );
}
/*
* LOAD functions.php and copy actions attached to init and after_theme_setup
* I know this is not 100% but should cover most cases of registering custom post types and taxonomies
*/
// check if this is a child theme
if ( get_stylesheet_directory() <> get_template_directory() ) {
// if functions.php doesn't exist (in the child theme and the parent theme), abort
if ( ! file_exists( get_template_directory() . '/functions.php' ) && ! file_exists( get_stylesheet_directory() . '/functions.php' ) )
return;
// load the child theme first
if ( file_exists( get_stylesheet_directory() . '/functions.php' ) )
require_once( get_stylesheet_directory() . '/functions.php' );
// load the parent theme second
if ( file_exists( get_template_directory() . '/functions.php' ) )
require_once( get_template_directory() . '/functions.php' );
} else {
// if there's no functions.php, abort
if ( ! file_exists( get_template_directory() . '/functions.php' ) )
return;
require_once( get_template_directory() . '/functions.php' );
}
copy_hooks( 'init', 'vip_jobs_init', get_stylesheet_directory() );
do_action( 'vip_jobs_init' );
copy_hooks( 'after_theme_setup', 'vip_jobs_after_theme_setup', get_stylesheet_directory() );
do_action( 'vip_jobs_after_theme_setup' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment