| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstrap |
| 4 |
* |
| 5 |
* @namespace UsabilityDynamics |
| 6 |
* |
| 7 |
* This file can be used to bootstrap any of the UD plugins. |
| 8 |
*/ |
| 9 |
namespace UsabilityDynamics\WP { |
| 10 |
|
| 11 |
if( !class_exists( 'UsabilityDynamics\WP\Bootstrap_Plugin' ) ) { |
| 12 |
|
| 13 |
/** |
| 14 |
* Bootstrap the plugin in WordPress. |
| 15 |
* |
| 16 |
* @class Bootstrap |
| 17 |
* @author: peshkov@UD |
| 18 |
*/ |
| 19 |
class Bootstrap_Plugin extends Bootstrap { |
| 20 |
|
| 21 |
public static $version = '1.0.4'; |
| 22 |
|
| 23 |
public $type = 'plugin'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor |
| 27 |
* Attention: MUST NOT BE CALLED DIRECTLY! USE get_instance() INSTEAD! |
| 28 |
* |
| 29 |
* @author peshkov@UD |
| 30 |
*/ |
| 31 |
protected function __construct( $args ) { |
| 32 |
parent::__construct( $args ); |
| 33 |
//** Maybe define license client */ |
| 34 |
$this->define_license_client(); |
| 35 |
//** Load text domain */ |
| 36 |
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ), 1 ); |
| 37 |
//** May be initialize Licenses Manager. */ |
| 38 |
add_action( 'plugins_loaded', array( $this, 'define_license_manager' ), 1 ); |
| 39 |
//** Initialize plugin here. All plugin actions must be added on this step */ |
| 40 |
add_action( 'plugins_loaded', array( $this, 'pre_init' ), 100 ); |
| 41 |
//** TGM Plugin activation. */ |
| 42 |
add_action( 'plugins_loaded', array( $this, 'check_plugins_requirements' ), 10 ); |
| 43 |
$this->boot(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Determine if we have errors before plugin initialization! |
| 48 |
* |
| 49 |
* @since 1.0.3 |
| 50 |
*/ |
| 51 |
public function pre_init() { |
| 52 |
//** Be sure we do not have errors. Do not initialize plugin if we have them. */ |
| 53 |
if( $this->has_errors() ) { |
| 54 |
return null; |
| 55 |
} |
| 56 |
$this->init(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns absolute DIR or URL path |
| 61 |
* |
| 62 |
* @since 1.0.2 |
| 63 |
* |
| 64 |
* @param $short_path |
| 65 |
* @param string $type |
| 66 |
* |
| 67 |
* @return bool|string |
| 68 |
*/ |
| 69 |
public function path( $short_path, $type = 'url' ) { |
| 70 |
switch( $type ) { |
| 71 |
case 'url': |
| 72 |
return $this->root_url . ltrim( $short_path, '/\\' ); |
| 73 |
break; |
| 74 |
case 'dir': |
| 75 |
return $this->root_path . ltrim( $short_path, '/\\' ); |
| 76 |
break; |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Called in the end of constructor. |
| 83 |
* Redeclare the method in child class! |
| 84 |
* |
| 85 |
* @author peshkov@UD |
| 86 |
*/ |
| 87 |
public function boot() {} |
| 88 |
|
| 89 |
/** |
| 90 |
* Load Text Domain |
| 91 |
* |
| 92 |
* @author peshkov@UD |
| 93 |
*/ |
| 94 |
public function load_textdomain() { |
| 95 |
load_plugin_textdomain( $this->domain, false, dirname( plugin_basename( $this->boot_file ) ) . '/static/languages/' ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Determine if instance already exists and Return Instance |
| 100 |
* |
| 101 |
* Attention: The method MUST be called from plugin core file at first to set correct path to plugin! |
| 102 |
* |
| 103 |
* @author peshkov@UD |
| 104 |
*/ |
| 105 |
public static function get_instance( $args = array() ) { |
| 106 |
$class = get_called_class(); |
| 107 |
//** We must be sure that final class contains static property $instance to prevent issues. */ |
| 108 |
if( !property_exists( $class, 'instance' ) ) { |
| 109 |
exit( "{$class} must have property \$instance" ); |
| 110 |
} |
| 111 |
$prop = new \ReflectionProperty( $class, 'instance' ); |
| 112 |
if( !$prop->isStatic() ) { |
| 113 |
exit( "Property \$instance must be <b>static</b> for {$class}" ); |
| 114 |
} |
| 115 |
if( null === $class::$instance ) { |
| 116 |
$dbt = debug_backtrace(); |
| 117 |
if( !empty( $dbt[0]['file'] ) && file_exists( $dbt[0]['file'] ) ) { |
| 118 |
$pd = get_file_data( $dbt[0]['file'], array( |
| 119 |
'name' => 'Plugin Name', |
| 120 |
'version' => 'Version', |
| 121 |
'domain' => 'Text Domain', |
| 122 |
'uservoice_url' => 'UserVoice', |
| 123 |
'support_url' => 'Support', |
| 124 |
), 'plugin' ); |
| 125 |
$args = array_merge( (array)$pd, (array)$args, array( |
| 126 |
'root_path' => dirname( $dbt[0]['file'] ), |
| 127 |
'root_url' => plugin_dir_url( $dbt[0]['file'] ), |
| 128 |
'schema_path' => dirname( $dbt[0]['file'] ) . '/composer.json', |
| 129 |
'boot_file' => $dbt[0]['file'], |
| 130 |
) ); |
| 131 |
$class::$instance = new $class( $args ); |
| 132 |
//** Register activation hook */ |
| 133 |
register_activation_hook( $dbt[0]['file'], array( $class::$instance, '_activate' ) ); |
| 134 |
//** Register activation hook */ |
| 135 |
register_deactivation_hook( $dbt[0]['file'], array( $class::$instance, '_deactivate' ) ); |
| 136 |
} else { |
| 137 |
$class::$instance = new $class( $args ); |
| 138 |
} |
| 139 |
} |
| 140 |
return $class::$instance; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Plugin Activation |
| 145 |
* Internal method. Use activate() instead |
| 146 |
*/ |
| 147 |
public function _activate() { |
| 148 |
/* Delete 'Install/Upgrade' notice 'dismissed' information */ |
| 149 |
delete_option( sanitize_key( 'dismiss_' . $this->slug . '_' . str_replace( '.', '_', $this->args['version'] ) . '_notice' ) ); |
| 150 |
/* Delete 'Bootstrap' notice 'dismissed' information */ |
| 151 |
delete_option( 'dismissed_notice_' . sanitize_key( $this->name ) ); |
| 152 |
delete_option( 'dismissed_warning_' . sanitize_key( $this->name ) ); |
| 153 |
$this->activate(); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Plugin Deactivation |
| 158 |
* Internal method. Use deactivate() instead |
| 159 |
*/ |
| 160 |
public function _deactivate() { |
| 161 |
/* Delete 'Install/Upgrade' notice 'dismissed' information */ |
| 162 |
delete_option( sanitize_key( 'dismiss_' . $this->slug . '_' . str_replace( '.', '_', $this->args['version'] ) . '_notice' ) ); |
| 163 |
/* Delete 'Bootstrap' notice 'dismissed' information */ |
| 164 |
delete_option( 'dismissed_notice_' . sanitize_key( $this->name ) ); |
| 165 |
delete_option( 'dismissed_warning_' . sanitize_key( $this->name ) ); |
| 166 |
$this->deactivate(); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Plugin Activation |
| 171 |
* Redeclare the method in child class! |
| 172 |
*/ |
| 173 |
public function activate() {} |
| 174 |
|
| 175 |
/** |
| 176 |
* Plugin Deactivation |
| 177 |
* Redeclare the method in child class! |
| 178 |
*/ |
| 179 |
public function deactivate() {} |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
|