| 1 |
<?php |
| 2 |
|
| 3 |
namespace Barn2\Lib\Plugin; |
| 4 |
|
| 5 |
use Barn2\Lib\Util; |
| 6 |
|
| 7 |
/** |
| 8 |
* Basic implementation of the Plugin interface which stores core data about a |
| 9 |
* WordPress plugin (ID, version number, etc). Data is passed as an array on construction. |
| 10 |
* |
| 11 |
* @package Barn2/barn2-lib |
| 12 |
* @author Barn2 Plugins <info@barn2.co.uk> |
| 13 |
* @license GPL-3.0 |
| 14 |
* @copyright Barn2 Media Ltd |
| 15 |
* @version 1.2 |
| 16 |
*/ |
| 17 |
class Simple_Plugin implements Plugin { |
| 18 |
|
| 19 |
protected $file; |
| 20 |
protected $data; |
| 21 |
private $basename = null; |
| 22 |
private $dir_path = null; |
| 23 |
private $dir_url = null; |
| 24 |
|
| 25 |
public function __construct( array $data ) { |
| 26 |
$this->data = \array_merge( |
| 27 |
array( |
| 28 |
'name' => '', |
| 29 |
'version' => '', |
| 30 |
'file' => null, |
| 31 |
'is_woocommerce' => false, |
| 32 |
'is_edd' => false, |
| 33 |
'documentation_path' => '', |
| 34 |
'settings_path' => '', |
| 35 |
), $data |
| 36 |
); |
| 37 |
|
| 38 |
$this->data['documentation_path'] = \ltrim( $this->data['documentation_path'], '/' ); |
| 39 |
$this->data['settings_path'] = \ltrim( $this->data['settings_path'], '/' ); |
| 40 |
|
| 41 |
// WooCommerce plugins cannot be EDD plugins (and vice-versa). |
| 42 |
if ( $this->is_edd() ) { |
| 43 |
$this->data['is_woocommerce'] = false; |
| 44 |
} elseif ( $this->is_woocommerce() ) { |
| 45 |
$this->data['is_edd'] = false; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Gets the name of this plugin. |
| 51 |
* |
| 52 |
* @return string The plugin name. |
| 53 |
*/ |
| 54 |
public function get_name() { |
| 55 |
return $this->data['name']; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Gets the plugin version number (e.g. 1.3.2). |
| 60 |
* |
| 61 |
* @return string The version number. |
| 62 |
*/ |
| 63 |
public function get_version() { |
| 64 |
return $this->data['version']; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Gets the full path to the main plugin file. |
| 69 |
* |
| 70 |
* @return string The plugin file. |
| 71 |
*/ |
| 72 |
public function get_file() { |
| 73 |
return $this->data['file']; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Gets the slug for this plugin (e.g. my-plugin). |
| 78 |
* |
| 79 |
* @return string The plugin slug. |
| 80 |
*/ |
| 81 |
public function get_slug() { |
| 82 |
$dir_path = $this->get_dir_path(); |
| 83 |
return ! empty( $dir_path ) ? \basename( $dir_path ) : ''; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Gets the 'basename' for the plugin (e.g. my-plugin/my-plugin.php). |
| 88 |
* |
| 89 |
* @return string The plugin basename. |
| 90 |
*/ |
| 91 |
public function get_basename() { |
| 92 |
if ( null === $this->basename ) { |
| 93 |
$this->basename = ! empty( $this->data['file'] ) ? \plugin_basename( $this->data['file'] ) : ''; |
| 94 |
} |
| 95 |
return $this->basename; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get the full directory path to the plugin folder, with trailing slash (e.g. /wp-content/plugins/my-plugin/). |
| 100 |
* |
| 101 |
* @return string The plugin directory path. |
| 102 |
*/ |
| 103 |
public function get_dir_path() { |
| 104 |
if ( null === $this->dir_path ) { |
| 105 |
$this->dir_path = ! empty( $this->data['file'] ) ? \plugin_dir_path( $this->data['file'] ) : ''; |
| 106 |
} |
| 107 |
return $this->dir_path; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get the URL to the plugin folder. |
| 112 |
* |
| 113 |
* @return string (URL) |
| 114 |
*/ |
| 115 |
public function get_dir_url() { |
| 116 |
if ( null === $this->dir_url ) { |
| 117 |
$this->dir_url = ! empty( $this->data['file'] ) ? \plugin_dir_url( $this->data['file'] ) : ''; |
| 118 |
} |
| 119 |
return $this->dir_url; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Is this plugin a WooCommerce extension? |
| 124 |
* |
| 125 |
* @return boolean true if it's a WooCommerce extension. |
| 126 |
*/ |
| 127 |
public function is_woocommerce() { |
| 128 |
return (bool) $this->data['is_woocommerce']; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Is this plugin an Easy Digital Downloads extension? |
| 133 |
* |
| 134 |
* @return boolean true if it's an EDD extension. |
| 135 |
*/ |
| 136 |
public function is_edd() { |
| 137 |
return (bool) $this->data['is_edd']; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get the documentation URL for this plugin. |
| 142 |
* |
| 143 |
* @return string (URL) |
| 144 |
*/ |
| 145 |
public function get_documentation_url() { |
| 146 |
return esc_url( Util::KNOWLEDGE_BASE_URL . '/' . $this->data['documentation_path'] ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get the support URL for this plugin. |
| 151 |
* |
| 152 |
* @return string (URL) |
| 153 |
*/ |
| 154 |
public function get_support_url() { |
| 155 |
return Util::barn2_url( 'support-center/' ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get the settings page URL in the WordPress admin. |
| 160 |
* |
| 161 |
* @return string (URL) |
| 162 |
*/ |
| 163 |
public function get_settings_page_url() { |
| 164 |
return ! empty( $this->data['settings_path'] ) ? \admin_url( $this->data['settings_path'] ) : ''; |
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
|