Created
December 7, 2012 05:02
-
-
Save mikeschinkel/4230921 to your computer and use it in GitHub Desktop.
Code showing the difficulty to create a symlinkable base for a plugin in WordPress 3.4.x
This file contains 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 | |
class Symlinkable_Plugin_Base { | |
var $plugin_file; | |
var $plugin_id; | |
function __construct( $args = array() ) { | |
global $pagenow; | |
if ( 'plugins.php' == $pagenow && isset( $_GET['action'] ) && isset( $_GET['plugin'] ) ) { | |
global $plugin; | |
if ( ! isset( $plugin ) ) { | |
/* | |
* This plugin is being activated | |
*/ | |
$this->plugin_id = filter_input( INPUT_GET, 'plugin', FILTER_SANITIZE_STRING ); | |
$this->plugin_file = WP_PLUGIN_DIR . '/' . $this->plugin_id; | |
} else { | |
/* | |
* Another plugin is being activated | |
*/ | |
$this->plugin_file = WP_PLUGIN_DIR . '/' . $plugin; | |
$this->plugin_id = $plugin; | |
} | |
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); | |
add_action( "activate_{$this->plugin_id}", array( $this, 'activate_plugin' ), 0 ); | |
register_activation_hook( $this->plugin_id, array( $this, 'activate' ) ); | |
} else if ( 'plugins.php' == $pagenow && isset( $_GET['action'] ) && 'delete-selected' == $_GET['action'] | |
&& isset( $_POST['verify-delete'] ) && '1' == $_POST['verify-delete'] | |
&& isset( $_POST['checked'] ) && count( $_POST['checked'] ) ) { | |
if ( preg_match( '#^uninstall_(.*?)$#', current_filter(), $match ) ) { | |
$this->plugin_file = $match[1]; | |
} else { | |
// What a hack! I really need help from WordPress core here!!! :) | |
$backtrace = debug_backtrace(); | |
foreach( $backtrace as $index => $call ) { | |
if ( preg_match( '#/wp-admin/includes/plugin.php$#', $call['file'] ) ) { | |
$this->plugin_file = $backtrace[$index-1]['file']; | |
break; | |
} | |
} | |
} | |
$this->plugin_id = basename( dirname( $this->plugin_file ) ) . '/' . basename( $this->plugin_file ); | |
} else { | |
/** | |
* Grab the plugin file name from one the global values set when the plugin is included. | |
* @see: http://wordpress.stackexchange.com/questions/15202/plugins-in-symlinked-directories | |
* @see: http://wordpress.stackexchange.com/a/15204/89 | |
*/ | |
global $mu_plugin, $network_plugin, $plugin; | |
if ( isset( $mu_plugin ) ) { | |
$this->plugin_file = $mu_plugin; | |
} else if ( isset( $network_plugin ) ) { | |
$this->plugin_file = $network_plugin; | |
} else if ( isset( $plugin ) ) { | |
$this->plugin_file = $plugin; | |
} else { | |
trigger_error( sprintf( __( 'Plugin %s only works when loaded by WordPress.' ), $this->plugin_id ) ); | |
exit; | |
} | |
$this->plugin_id = basename( dirname( $this->plugin_file ) ) . '/' . basename( $this->plugin_file ); | |
require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); | |
if ( ! is_plugin_active( $this->plugin_id ) ) { | |
trigger_error( sprintf( __( 'Plugin %s is not an active plugin or is not installed in a subdirectory of %s.' ), | |
$this->plugin_name, | |
WP_PLUGIN_DIR | |
)); | |
exit; | |
} | |
register_deactivation_hook( $this->plugin_id, array( $this, 'deactivate' ) ); | |
} | |
register_uninstall_hook( $this->plugin_id, array( parent::plugin_class, 'uninstall' ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment