| 1 |
<?php |
| 2 |
/** |
| 3 |
* QuadLayers WP Notice Plugin Required |
| 4 |
* |
| 5 |
* @package quadlayers/wp-notice-plugin-required |
| 6 |
* @link https://github.com/quadlayers/wp-notice-plugin-required |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace QuadLayers\WP_Notice_Plugin_Required; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class Load |
| 13 |
* |
| 14 |
* @package QuadLayers\WP_Notice_Plugin_Required |
| 15 |
*/ |
| 16 |
class Load { |
| 17 |
|
| 18 |
/** |
| 19 |
* Required Plugins. |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $plugins; |
| 24 |
/** |
| 25 |
* Current Plugin name. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $current_plugin_name = ''; |
| 30 |
|
| 31 |
/** |
| 32 |
* Load constructor. |
| 33 |
* |
| 34 |
* @param string $current_plugin_name Current Plugin name. |
| 35 |
* @param array $plugins Required Plugins. |
| 36 |
*/ |
| 37 |
public function __construct( $current_plugin_name, array $plugins = array() ) { |
| 38 |
$this->current_plugin_name = $current_plugin_name; |
| 39 |
$this->plugins = $plugins; |
| 40 |
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Add admin notice. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function admin_notices() { |
| 49 |
|
| 50 |
$screen = get_current_screen(); |
| 51 |
|
| 52 |
if ( isset( $screen->parent_file ) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
foreach ( $this->plugins as $plugin ) { |
| 57 |
|
| 58 |
if ( ! isset( $plugin['slug'], $plugin['name'] ) ) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
$plugin = Plugin::get_instance( $plugin['slug'], $plugin['name'] ); |
| 63 |
|
| 64 |
$notice = $this->add_notice( $plugin ); |
| 65 |
|
| 66 |
/** |
| 67 |
* If notice is added then return. |
| 68 |
* This will prevent multiple notices for same plugin. |
| 69 |
*/ |
| 70 |
if ( $notice ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Add notice. |
| 78 |
* |
| 79 |
* @param Plugin $plugin Plugin. |
| 80 |
* |
| 81 |
* @return bool |
| 82 |
*/ |
| 83 |
private function add_notice( Plugin $plugin ) { |
| 84 |
|
| 85 |
if ( $plugin->is_plugin_activated() ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
if ( $plugin->is_plugin_installed() ) { |
| 90 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
?> |
| 94 |
<div class="error"> |
| 95 |
<p> |
| 96 |
<a href="<?php echo esc_url( $plugin->get_plugin_activate_link() ); ?>" class='button button - secondary'><?php printf( esc_html__( 'Activate % s', 'wp-notice-plugin-required' ), esc_html( $plugin->get_plugin_name() ) ); ?></a> |
| 97 |
<?php printf( esc_html__( 'The %1$s is not working because you need to activate the %2$s plugin. ', 'wp-notice-plugin-required' ), esc_html( $this->current_plugin_name ), esc_html( $plugin->get_plugin_name() ) ); ?> |
| 98 |
</p> |
| 99 |
</div> |
| 100 |
<?php |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
?> |
| 108 |
<div class="error"> |
| 109 |
<p> |
| 110 |
<a href="<?php echo esc_url( $plugin->get_plugin_install_link() ); ?>" class='button button - secondary'><?php printf( esc_html__( 'Install % s', 'wp-notice-plugin-required' ), esc_html( $plugin->get_plugin_name() ) ); ?></a> |
| 111 |
<?php printf( esc_html__( 'The %1$s is not working because you need to install the %2$s plugin. ', 'wp-notice-plugin-required' ), esc_html( $this->current_plugin_name ), esc_html( $plugin->get_plugin_name() ) ); ?> |
| 112 |
</p> |
| 113 |
</div> |
| 114 |
<?php |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|