| 1 |
<?php |
| 2 |
|
| 3 |
namespace Preseto\WidgetContext; |
| 4 |
|
| 5 |
/** |
| 6 |
* WordPress plugin interface. |
| 7 |
*/ |
| 8 |
class Plugin { |
| 9 |
|
| 10 |
/** |
| 11 |
* Absolute path to the main plugin file. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $file; |
| 16 |
|
| 17 |
/** |
| 18 |
* Absolute path to the root directory of this plugin. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $dir; |
| 23 |
|
| 24 |
/** |
| 25 |
* Store the WP uploads dir object. |
| 26 |
* |
| 27 |
* @see https://developer.wordpress.org/reference/functions/wp_upload_dir/ |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected $uploads_dir; |
| 31 |
|
| 32 |
/** |
| 33 |
* Setup the plugin. |
| 34 |
* |
| 35 |
* @param string $plugin_file_path Absolute path to the main plugin file. |
| 36 |
*/ |
| 37 |
public function __construct( $plugin_file_path ) { |
| 38 |
$this->file = $plugin_file_path; |
| 39 |
|
| 40 |
$this->dir = dirname( $plugin_file_path ); |
| 41 |
$this->uploads_dir = wp_upload_dir( null, false ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Return the absolute path to the plugin directory. |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function dir() { |
| 50 |
return $this->dir; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Return the absolute path to the plugin file. |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function file() { |
| 59 |
return $this->file; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get the file path relative to the WordPress plugin directory. |
| 64 |
* |
| 65 |
* @param string $file_path Absolute path to any plugin file. |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
public function basename( $file_path = null ) { |
| 70 |
if ( ! isset( $file_path ) ) { |
| 71 |
$file_path = $this->file(); |
| 72 |
} |
| 73 |
|
| 74 |
return plugin_basename( $file_path ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get the public URL to the asset file. |
| 79 |
* |
| 80 |
* @param string $path_relative Path relative to this plugin directory root. |
| 81 |
* @return string The URL to the asset. |
| 82 |
*/ |
| 83 |
public function asset_url( $path_relative ) { |
| 84 |
return plugins_url( $path_relative, $this->file() ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get absolute path to a file in the uploads directory. |
| 89 |
* |
| 90 |
* @param string $path_relative File path relative to the root of the WordPress uploads directory. |
| 91 |
* |
| 92 |
* @return string |
| 93 |
*/ |
| 94 |
public function uploads_dir( $path_relative = null ) { |
| 95 |
if ( isset( $path_relative ) ) { |
| 96 |
return sprintf( '%s/%s', $this->uploads_dir['basedir'], $path_relative ); |
| 97 |
} |
| 98 |
|
| 99 |
return $this->uploads_dir['basedir']; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get URL to a file in the uploads directory. |
| 104 |
* |
| 105 |
* @param string $path_relative Path to the file relative to the root of the WordPress uploads directory. |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function uploads_dir_url( $path_relative = null ) { |
| 110 |
if ( isset( $path_relative ) ) { |
| 111 |
return sprintf( '%s/%s', $this->uploads_dir['baseurl'], $path_relative ); |
| 112 |
} |
| 113 |
|
| 114 |
return $this->uploads_dir['baseurl']; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Is WP debug mode enabled. |
| 119 |
* |
| 120 |
* @return boolean |
| 121 |
*/ |
| 122 |
public function is_debug() { |
| 123 |
return ( defined( 'WP_DEBUG' ) && WP_DEBUG ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Is WP script debug mode enabled. |
| 128 |
* |
| 129 |
* @return boolean |
| 130 |
*/ |
| 131 |
public function is_script_debug() { |
| 132 |
return ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Return the current version of the plugin. |
| 137 |
* |
| 138 |
* @return mixed |
| 139 |
*/ |
| 140 |
public function version() { |
| 141 |
return $this->meta( 'Version' ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Sync the plugin version with the asset version. |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
public function asset_version() { |
| 150 |
if ( $this->is_debug() || $this->is_script_debug() ) { |
| 151 |
return time(); |
| 152 |
} |
| 153 |
|
| 154 |
return $this->version(); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get plugin meta data. |
| 159 |
* |
| 160 |
* @param string $field Optional field key. |
| 161 |
* |
| 162 |
* @return array|string|null |
| 163 |
*/ |
| 164 |
public function meta( $field = null ) { |
| 165 |
static $meta; |
| 166 |
|
| 167 |
if ( ! isset( $meta ) ) { |
| 168 |
$meta = get_file_data( |
| 169 |
$this->file, |
| 170 |
array( |
| 171 |
'Version' => 'Version', |
| 172 |
) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
if ( isset( $field ) ) { |
| 177 |
if ( isset( $meta[ $field ] ) ) { |
| 178 |
return $meta[ $field ]; |
| 179 |
} |
| 180 |
|
| 181 |
return null; |
| 182 |
} |
| 183 |
|
| 184 |
return $meta; |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|