.
* @since 1.0.0
*/
namespace POSessions\Plugin;
use POSessions\System\Environment;
use POSessions\System\Loader;
use POSessions\System\I18n;
use POSessions\System\Assets;
use POSessions\Library\Libraries;
use POSessions\System\Cache;
use POSessions\System\Nag;
use POSessions\System\Session;
use POSessions\Plugin\Feature\Analytics;
use POSessions\System\Option;
/**
* The core plugin class.
*
* This is used to define internationalization, admin-specific hooks, and
* public-facing site hooks.
*
* @package Plugin
* @author Pierre Lannoy .
* @since 1.0.0
*/
class Core {
/**
* The loader that's responsible for maintaining and registering all hooks that power
* the plugin.
*
* @since 1.0.0
* @access protected
* @var Loader $loader Maintains and registers all hooks for the plugin.
*/
protected $loader;
/**
* Define the core functionality of the plugin.
*
* Load the dependencies, define the locale, and set the hooks for the admin area and
* the public-facing side of the site.
*
* @since 1.0.0
*/
public function __construct() {
$this->loader = new Loader();
$this->define_global_hooks();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_custom_messages();
if ( \DecaLog\Engine::isDecalogActivated() && Option::network_get( 'metrics' ) && Environment::exec_mode_for_metrics() && Option::network_get( 'analytics' ) ) {
$this->define_metrics();
}
}
/**
* Register all of the hooks related to the features of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_global_hooks() {
$bootstrap = new Initializer();
$assets = new Assets();
$updater = new Updater();
$libraries = new Libraries();
$this->loader->add_action( 'init', 'POSessions\Plugin\Integration\Databeam', 'init' );
$this->loader->add_filter( 'perfopsone_plugin_info', self::class, 'perfopsone_plugin_info' );
$this->loader->add_action( 'init', $bootstrap, 'initialize' );
$this->loader->add_action( 'init', $bootstrap, 'late_initialize', PHP_INT_MAX );
$this->loader->add_action( 'wp_head', $assets, 'prefetch' );
add_shortcode( 'pose-changelog', [ $updater, 'sc_get_changelog' ] );
add_shortcode( 'pose-libraries', [ $libraries, 'sc_get_list' ] );
add_shortcode( 'pose-statistics', [ 'POSessions\System\Statistics', 'sc_get_raw' ] );
}
/**
* Register all of the hooks related to the admin area functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_admin_hooks() {
$plugin_admin = new Sessions_Admin();
$nag = new Nag();
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'init_admin_menus' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'finalize_admin_menus', 100 );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'normalize_admin_menus', 110 );
$this->loader->add_action( 'admin_init', $plugin_admin, 'init_settings_sections' );
$this->loader->add_filter( 'plugin_action_links_' . plugin_basename( POSE_PLUGIN_DIR . POSE_SLUG . '.php' ), $plugin_admin, 'add_actions_links', 10, 4 );
$this->loader->add_filter( 'plugin_row_meta', $plugin_admin, 'add_row_meta', 10, 2 );
$this->loader->add_action( 'admin_notices', $nag, 'display' );
$this->loader->add_action( 'wp_ajax_hide_pose_nag', $nag, 'hide_callback' );
$this->loader->add_action( 'wp_ajax_pose_get_stats', 'POSessions\Plugin\Feature\AnalyticsFactory', 'get_stats_callback' );
}
/**
* Register all of the hooks related to the public-facing functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_public_hooks() {
$plugin_public = new Sessions_Public();
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
}
/**
* Register all of the filters related to messages customization.
*
* @since 1.0.0
* @access private
*/
private function define_custom_messages() {
if ( '' !== Option::network_get( 'blocked_message' ) ) {
add_filter(
'sessions_blocked_message',
function( $message ) {
return Option::network_get( 'blocked_message' );
},
0,
1
);
}
if ( '' !== Option::network_get( 'bad_ip_message' ) ) {
add_filter(
'sessions_bad_ip_message',
function( $message ) {
return Option::network_get( 'bad_ip_message' );
},
0,
1
);
}
}
/**
* Register all metrics of the plugin.
*
* @since 1.2.0
* @access private
*/
private function define_metrics() {
$span = \DecaLog\Engine::tracesLogger( POSE_SLUG )->startSpan( 'Metrics collation', DECALOG_SPAN_PLUGINS_LOAD );
$cache_id = 'metrics/lastcheck';
$analytics = Cache::get_global( $cache_id );
if ( ! isset( $analytics ) ) {
$analytics = Analytics::get_status_kpi_collection( [ 'site_id' => 0 ] );
Cache::set_global( $cache_id, $analytics, 'metrics' );
}
if ( isset( $analytics ) ) {
$metrics = \DecaLog\Engine::metricsLogger( POSE_SLUG );
if ( array_key_exists( 'data', $analytics ) ) {
foreach ( $analytics['data'] as $kpi ) {
$m = $kpi['metrics'] ?? null;
if ( isset( $m ) ) {
switch ( $m['type'] ) {
case 'gauge':
$metrics->createProdGauge( $m['name'], $m['value'], $m['desc'] );
break;
case 'counter':
$metrics->createProdCounter( $m['name'], $m['desc'] );
$metrics->incProdCounter( $m['name'], $m['value'] );
break;
}
}
}
}
}
\DecaLog\Engine::tracesLogger( POSE_SLUG )->endSpan( $span );
}
/**
* Run the loader to execute all of the hooks with WordPress.
*
* @since 1.0.0
*/
public function run() {
$this->loader->run();
}
/**
* The reference to the class that orchestrates the hooks with the plugin.
*
* @since 1.0.0
* @return Loader Orchestrates the hooks of the plugin.
*/
public function get_loader() {
return $this->loader;
}
/**
* Adds full plugin identification.
*
* @param array $plugin The already set identification information.
* @return array The extended identification information.
* @since 1.0.0
*/
public static function perfopsone_plugin_info( $plugin ) {
$plugin[ POSE_SLUG ] = [
'name' => POSE_PRODUCT_NAME,
'code' => POSE_CODENAME,
'version' => POSE_VERSION,
'url' => POSE_PRODUCT_URL,
'icon' => self::get_base64_logo(),
];
return $plugin;
}
/**
* Returns a base64 svg resource for the plugin logo.
*
* @return string The svg resource as a base64.
* @since 1.0.0
*/
public static function get_base64_logo() {
$source = '';
// phpcs:ignore
return 'data:image/svg+xml;base64,' . base64_encode( $source );
}
}