Your IP : 216.73.217.81
<?php
namespace Preseto\WidgetContext;
/**
* WordPress plugin interface.
*/
class Plugin {
/**
* Absolute path to the main plugin file.
*
* @var string
*/
protected string $file;
/**
* Store the WP uploads dir object.
*
* @see https://developer.wordpress.org/reference/functions/wp_upload_dir/
* @var array
*/
protected array $uploads_dir;
/**
* Plugin header data.
*
* @var array
*/
protected array $meta;
/**
* Setup the plugin.
*
* @param string $plugin_file_path Absolute path to the main plugin file.
*/
public function __construct( string $plugin_file_path ) {
$this->file = $plugin_file_path;
$this->uploads_dir = wp_upload_dir( null, false );
}
/**
* Return the absolute path to the plugin directory.
*
* @return string
*/
public function dir(): string {
return dirname( $this->file );
}
/**
* Return the absolute path to the plugin file.
*
* @return string
*/
public function file(): string {
return $this->file;
}
/**
* Get the file path relative to the WordPress plugin directory.
*
* @param string $file_path Absolute path to any plugin file.
*
* @return string
*/
public function basename( ?string $file_path = null ): string {
if ( ! isset( $file_path ) ) {
$file_path = $this->file();
}
return plugin_basename( $file_path );
}
/**
* Get the public URL to the asset file.
*
* @param string $path_relative Path relative to this plugin directory root.
* @return string The URL to the asset.
*/
public function asset_url( string $path_relative ): string {
return plugins_url( $path_relative, $this->file() );
}
/**
* Get absolute path to a file in the uploads directory.
*
* @param string $path_relative File path relative to the root of the WordPress uploads directory.
*
* @return string
*/
public function uploads_dir( ?string $path_relative = null ): string {
if ( isset( $path_relative ) ) {
return sprintf( '%s/%s', $this->uploads_dir['basedir'], $path_relative );
}
return $this->uploads_dir['basedir'];
}
/**
* Get URL to a file in the uploads directory.
*
* @param string $path_relative Path to the file relative to the root of the WordPress uploads directory.
*
* @return string
*/
public function uploads_dir_url( ?string $path_relative = null ): string {
if ( isset( $path_relative ) ) {
return sprintf( '%s/%s', $this->uploads_dir['baseurl'], $path_relative );
}
return $this->uploads_dir['baseurl'];
}
/**
* Is WP debug mode enabled.
*
* @return boolean
*/
public function is_debug(): bool {
return ( defined( 'WP_DEBUG' ) && WP_DEBUG );
}
/**
* Is WP script debug mode enabled.
*
* @return boolean
*/
public function is_script_debug(): bool {
return ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG );
}
/**
* Return the current version of the plugin.
*
* @return mixed
*/
public function version(): ?string {
return $this->meta( 'Version' );
}
/**
* Sync the plugin version with the asset version.
*
* @return string
*/
public function asset_version(): ?string {
if ( $this->is_debug() || $this->is_script_debug() ) {
return (string) time();
}
return $this->version();
}
/**
* Get plugin meta data.
*
* @param string $field Optional field key.
*
* @return array|string|null
*/
public function meta( ?string $field = null ) {
if ( ! isset( $this->meta ) ) {
$this->meta = get_file_data(
$this->file,
array(
'Version' => 'Version',
)
);
}
if ( isset( $field ) ) {
if ( isset( $this->meta[ $field ] ) ) {
return (string) $this->meta[ $field ];
}
return null;
}
return $this->meta;
}
}