| 1 |
<?php |
| 2 |
namespace Depicter\WordPress; |
| 3 |
|
| 4 |
use Averta\WordPress\Models\WPOptions; |
| 5 |
use WPEmerge\ServiceProviders\ServiceProviderInterface; |
| 6 |
|
| 7 |
/** |
| 8 |
* Register plugin general hooks. |
| 9 |
*/ |
| 10 |
class PluginServiceProvider implements ServiceProviderInterface |
| 11 |
{ |
| 12 |
/** |
| 13 |
* {@inheritDoc} |
| 14 |
*/ |
| 15 |
public function register( $container ) { |
| 16 |
$app = $container[ WPEMERGE_APPLICATION_KEY ]; |
| 17 |
|
| 18 |
// register depicter options |
| 19 |
$container[ 'depicter.options' ] = function () { |
| 20 |
return new WPOptions('depicter_'); |
| 21 |
}; |
| 22 |
$app->alias( 'options', 'depicter.options' ); |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* {@inheritDoc} |
| 28 |
*/ |
| 29 |
public function bootstrap( $container ) { |
| 30 |
register_activation_hook( DEPICTER_PLUGIN_FILE, [ $this, 'activate' ] ); |
| 31 |
register_deactivation_hook(DEPICTER_PLUGIN_FILE, [ $this, 'deactivate'] ); |
| 32 |
|
| 33 |
add_action( 'plugins_loaded', [$this, 'loadTextDomain'] ); |
| 34 |
add_action( 'upgrader_process_complete',[ $this, 'check_plugin_upgrade' ],10, 2 ); |
| 35 |
add_action( 'admin_init', [ $this, 'check_plugin_upgrade_via_upload' ] ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Plugin activation. |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function activate() { |
| 44 |
// Nothing to do right now. |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Plugin deactivation. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function deactivate() { |
| 53 |
// Nothing to do right now. |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Load text domain. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function loadTextDomain() { |
| 62 |
load_plugin_textdomain( 'depicter', false, basename( dirname( DEPICTER_PLUGIN_FILE ) ) . DIRECTORY_SEPARATOR . 'languages' ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Check and fire plugin update hook if plugin upgraded |
| 67 |
* |
| 68 |
* @param $upgraderObject |
| 69 |
* @param $options |
| 70 |
*/ |
| 71 |
public function check_plugin_upgrade( $upgraderObject, $options ) { |
| 72 |
|
| 73 |
if ( $options['action'] == 'update' && $options['type'] == 'plugin' ) { |
| 74 |
if( !empty( $options['plugins'] ) ){ |
| 75 |
foreach( $options['plugins'] as $plugin) { |
| 76 |
if ( $plugin == DEPICTER_PLUGIN_BASENAME ) { |
| 77 |
$previousVersion = \Depicter::options()->get( 'version', 0 ); |
| 78 |
\Depicter::options()->set( 'version_previous', $previousVersion ); |
| 79 |
\Depicter::options()->set( 'version', DEPICTER_VERSION ); |
| 80 |
do_action( 'depicter/plugin/updated' ); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if plugin updated via upload or not |
| 89 |
*/ |
| 90 |
public function check_plugin_upgrade_via_upload() { |
| 91 |
$previousVersion = \Depicter::options()->get( 'version', 0 ); |
| 92 |
if ( version_compare( DEPICTER_VERSION, $previousVersion, '>' ) ) { |
| 93 |
\Depicter::options()->set( 'version_previous', $previousVersion ); |
| 94 |
\Depicter::options()->set( 'version', DEPICTER_VERSION ); |
| 95 |
do_action( 'depicter/plugin/updated' ); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|