public

Code showing the difficulty to create a symlinkable base for a plugin in WordPress 3.4.x

  • Download Gist
symlinkable-plugin-base.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<?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' ) );
 
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.