| 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 |
$container[ 'depicter.wp.cli.service' ] = function () { |
| 25 |
return new WPCliService(); |
| 26 |
}; |
| 27 |
$app->alias( 'cli', 'depicter.wp.cli.service' ); |
| 28 |
|
| 29 |
$container[ 'depicter.document.detector.service' ] = function () { |
| 30 |
return new DocumentDetectorService(); |
| 31 |
}; |
| 32 |
$app->alias( 'documentDetector', 'depicter.document.detector.service' ); |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* {@inheritDoc} |
| 38 |
*/ |
| 39 |
public function bootstrap( $container ) { |
| 40 |
register_activation_hook( DEPICTER_PLUGIN_FILE, [ $this, 'activate' ] ); |
| 41 |
register_deactivation_hook(DEPICTER_PLUGIN_FILE, [ $this, 'deactivate'] ); |
| 42 |
|
| 43 |
add_action( 'admin_init', [ $this, 'check_plugin_upgrade_via_upload' ] ); |
| 44 |
add_action( 'admin_init', [ $this, 'check_redirect_process' ] ); |
| 45 |
add_filter( 'update_plugin_complete_actions', [ $this, 'add_depicter_link_after_upgrade'], 10, 2); |
| 46 |
|
| 47 |
add_action('wp_enqueue_scripts', [ $this, 'enqueueGlobalScripts'], 99 ); |
| 48 |
|
| 49 |
add_action('admin_bar_menu', [ $this, 'initAdminBarMenu'], 99); |
| 50 |
|
| 51 |
if ( defined('WP_CLI') && WP_CLI ) { |
| 52 |
\WP_CLI::add_command( 'depicter', \Depicter::app()->cli() ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Plugin activation. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function activate() { |
| 64 |
set_transient( 'depicter_do_activation_redirect', true, 60 ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Plugin deactivation. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function deactivate() { |
| 73 |
// Nothing to do right now. |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Check if plugin updated via upload or not |
| 78 |
*/ |
| 79 |
public function check_plugin_upgrade_via_upload() { |
| 80 |
$previousVersion = \Depicter::options()->get( 'version', 0 ); |
| 81 |
if ( version_compare( DEPICTER_VERSION, $previousVersion, '>' ) ) { |
| 82 |
\Depicter::options()->set( 'version_previous', $previousVersion ); |
| 83 |
\Depicter::options()->set( 'version', DEPICTER_VERSION ); |
| 84 |
do_action( 'depicter/plugin/updated' ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Add go to depicter dashboard link after upgrade at bottom of upgrade page |
| 90 |
* |
| 91 |
* @param array $install_actions |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function add_depicter_link_after_upgrade( $install_actions, $plugin ){ |
| 95 |
if ( $plugin !== 'depicter/depicter.php' ) { |
| 96 |
return $install_actions; |
| 97 |
} |
| 98 |
|
| 99 |
$install_actions['depicter_dashboard'] = sprintf( |
| 100 |
'<a href="%s" target="_parent">%s</a>', |
| 101 |
admin_url( 'admin.php?page=depicter-dashboard' ), |
| 102 |
__( 'Go to Depicter', 'depicter' ) |
| 103 |
); |
| 104 |
|
| 105 |
return $install_actions; |
| 106 |
} |
| 107 |
|
| 108 |
public function check_redirect_process() { |
| 109 |
// Check if the transient is set |
| 110 |
if ( get_transient('depicter_do_activation_redirect') ) { |
| 111 |
// Delete the transient to prevent repeated redirects |
| 112 |
delete_transient('depicter_do_activation_redirect'); |
| 113 |
|
| 114 |
if ( \Depicter::client()->isNewClient() && ! wp_doing_ajax() ) { |
| 115 |
// Redirect to the desired page |
| 116 |
wp_safe_redirect( admin_url('admin.php?page=depicter-dashboard') ); |
| 117 |
exit; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public function enqueueGlobalScripts() { |
| 123 |
if ( \Depicter::documentDetector()->hasAnyDocument() ) { |
| 124 |
wp_add_inline_script( 'depicter--player', "window.depicterConfig = " . wp_json_encode([ |
| 125 |
"ajaxApiUrl" => esc_url( admin_url( 'admin-ajax.php' ) ) |
| 126 |
]), "before"); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function initAdminBarMenu( $wpAdminBar ){ |
| 131 |
|
| 132 |
if ( \Depicter::documentDetector()->hasAnyDocument() ) { |
| 133 |
$wpAdminBar->add_node([ |
| 134 |
'id' => 'depicter-menu', |
| 135 |
'title' => __( 'Depicter', 'depicter' ), |
| 136 |
'href' => '#', |
| 137 |
]); |
| 138 |
foreach ( \Depicter::documentDetector()->getDocumentsList() as $documentID ) { |
| 139 |
$wpAdminBar->add_node([ |
| 140 |
'id' => 'depicter-submenu-' . $documentID, |
| 141 |
'title' => \Depicter::documentRepository()->getFieldValue( $documentID, 'name' ), |
| 142 |
'parent' => 'depicter-menu', |
| 143 |
'href' => admin_url('post.php?document=' . $documentID . '&action=depicter') |
| 144 |
]); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|