HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/appointmentbook.me/wp-content/plugins/floating-awesome-button/src/Plugin/Plugin.php
<?php

namespace Fab;

use Fab\Feature\Upsell;

! defined( 'WPINC ' ) || die;

/**
 * Initiate plugins
 *
 * @package    Fab
 * @subpackage Fab\Includes
 */

class Plugin {

    /**
     * @var     Plugin  $instance   Hold instance
     */
    private static $instance = null;

    /**
     * Plugin name
     *
     * @var     string
     */
    protected $name;

    /**
     * Plugin slug
     *
     * @var     string
     */
    protected $slug;

    /**
     * Plugin version
     *
     * @var     string
     */
    protected $version;

    /**
     * Plugin stage (true = production, false = development)
     *
     * @var     boolean
     */
    protected $production;

    /**
     * Enable/Disable plugins hook (Action, Filter, Shortcode)
     *
     * @var     array   ['action', 'filter', 'shortcode']
     */
    protected $enableHooks;

    /**
     * Plugin path
     *
     * @var     array
     */
    protected $path;

    /**
     * Lists of plugin apis
     *
     * @var     array
     */
    protected $apis;

    /**
     * Lists of plugin controllers
     *
     * @var     array
     */
    protected $controllers;

    /**
     * Lists of plugin features
     *
     * @var     array
     */
    protected $features;

    /**
     * Lists of plugin models
     *
     * @var     array
     */
    protected $models;

    /**
     * Plugin configuration
     *
     * @var     object
     */
    protected $config;

    /**
     * @access   protected
     * @var      object    $helper  Helper object for controller
     */
    protected $Helper;

    /**
     * @access   protected
     * @var      object    $form  Form object for controller
     */
    protected $Form;

    /**
     * @access   protected
     * @var      object    $helper  Helper object for controller
     */
    protected $WP;

    /**
     * Define the core functionality of the plugin.
     *
     * @param   array $path     WordPress plugin path
     * @return void
     */
    public function __construct() {
        /** Initiate Plugin */
        $this->config      = $this->getPluginConfig();
        $this->name        = $this->config->name;
        $this->slug        = strtolower( $this->config->name );
        $this->slug        = str_replace( ' ', '-', $this->slug );
        $this->version     = $this->config->version;
        $this->production  = $this->config->production;
        $this->enableHooks = $this->config->enableHooks;
        $this->controllers = array();
        $this->features    = array();
        $this->models      = array();
        $this->Helper      = new Helper();
        $this->Form        = new Form();
        $this->WP          = new \Fab\Wordpress\Helper();
        /** Init Config */
        $this->config->path = explode( DIRECTORY_SEPARATOR, dirname( __DIR__, 2 ) );
        $this->config->path = implode( DIRECTORY_SEPARATOR, $this->config->path ) . DIRECTORY_SEPARATOR . end( $this->config->path ) . '.php';
        $this->config->options = $this->WP->get_option( 'fab_config' );
        $this->config->options = ( $this->config->options ) ? $this->config->options : new \stdClass();
        $this->path            = $this->WP->getPath( $this->config->path );
    }

    /**
     * Get Plugin Config
     */
    public function getPluginConfig() {
        $config = dirname( __DIR__, 2 );
        $config = file_get_contents( $config . '/config.json' );
        $config = json_decode( $config );
        return $config;
    }

    /**
     * Run the plugin
     * - Load plugin model
     * - Load plugin API
     * - Load plugin controller
     *
     * @return  void
     */
    public function run() {
        $this->Helper->defineConst( $this );
        $this->loadModels();
        $this->loadFeatures();
        $this->loadHooks( 'Controller' );
        $this->loadHooks( 'Api' );
        $this->loadModulesHooks();
        $this->setDefaultOption();
    }

    /**
     * Lifecycle Activate the plugin
     *
     * @return  void
     */
    public function activate() {
        $this->setDefaultOption();
    }

    /**
     * Load registered models
     *
     * @return  void
     */
    public function loadModels() {
        $models = $this->Helper->getDirFiles( $this->path['plugin_path'] . 'src/Model' );
        $allow  = array( '.', '..', '.DS_Store', 'index.php' );
        foreach ( $models as $model ) {
            if ( in_array( basename( $model ), $allow ) ) {
                continue;
            }
            $name  = basename( $model, '.php' );
            $model = '\\Fab\\Model\\' . $name;
            $model = new $model( $this );
            /** Build */
            $args          = $model->getArgs();
            $args['build'] = ( isset( $args['build'] ) ) ? $args['build'] : true;
            if ( $args['build'] ) {
                $model->build();
            }
            /** Run Hooks */
            $this->models[ $name ] = $model;
            foreach ( $model->getHooks() as $hook ) {
                $class = str_replace( 'Fab\\Wordpress\\Hook\\', '', get_class( $hook ) );
                if ( in_array( strtolower( $class ), $this->enableHooks ) ) {
                    $hook->run();
                }
            }
        }
    }

    /**
     * Load registered hooks in a controller
     *
     * @return  void
     * @pattern bridge
     */
    private function loadFeatures() {
        $features = $this->Helper->getDirFiles( $this->path['plugin_path'] . 'src/Feature' );
        $allow    = array( '.', '..', '.DS_Store', 'index.php' );
        foreach ( $features as $feature ) {
            if ( in_array( basename( $feature ), $allow ) ) {
                continue;
            }
            $name                                 = basename( $feature, '.php' );
            $feature                              = '\\Fab\\Feature\\' . $name;
            $feature                              = new $feature( $this );
            $this->features[ $feature->getKey() ] = $feature;
            if( method_exists($feature, 'loadHooks') ) {
                foreach ( $feature->getHooks() as $hook ) { $hook->run(); }
            }
        }
        ksort( $this->features );
    }

    /**
     * Load Modules Hook
     */
    public function loadModulesHooks(){
        $modules = [];
        $allow = array( '.', '..', '.DS_Store', 'index.php' );
        foreach($this->Helper->getDirFiles( $this->path['plugin_path'] . 'src/Helper/FABModule' ) as $module){
            if ( in_array( basename( $module ), $allow ) ) { continue; }
            $name = basename( $module, '.php' );
            $class = '\\Fab\\Module\\' . $name;
            if( method_exists($class, 'loadHooks') ) {
                $modules[ $name ] = new $class();
                foreach ( $modules[ $name ]->getHooks() as $hook ) { $hook->run(); }
            }
        }
    }

    /**
     * Get FAB Modules
     */
    public function getModules(){
        $modules = [];
        $allow = array( '.', '..', '.DS_Store', 'index.php' );
        foreach($this->Helper->getDirFiles( $this->path['plugin_path'] . 'src/Helper/FABModule' ) as $module){
            if ( in_array( basename( $module ), $allow ) ) { continue; }
            $name = basename( $module, '.php' );
            $class = '\\Fab\\Module\\' . $name;
            $modules[ $name ] = new $class();
        }
        return $modules;
    }

    /**
     * Load registered hooks in a controller
     *
     * @return  void
     * @var     string  $dir   plugin hooks directory (API, Controller)
     * @pattern bridge
     */
    private function loadHooks( $dir ) {
        $controllers = $this->Helper->getDirFiles( $this->path['plugin_path'] . 'src/' . $dir );
        $allow       = array( '.', '..', '.DS_Store', 'index.php' );
        foreach ( $controllers as $controller ) {
            if ( in_array( basename( $controller ), $allow ) ) {
                continue;
            }
            $name       = basename( $controller, '.php' );
            $controller = '\\Fab\\' . ucwords( $dir ) . '\\' . $name;
            $controller = new $controller( $this );
            if ( $dir === 'Controller' ) {
                $this->controllers[ $name ] = $controller;
            }
            if ( $dir === 'Api' ) {
                $this->apis[ $name ] = $controller;
            }
            foreach ( $controller->getHooks() as $hook ) {
                $class = str_replace( 'Fab\\Wordpress\\Hook\\', '', get_class( $hook ) );
                if ( in_array( strtolower( $class ), $this->enableHooks ) ) {
                    $namespace    = ( new \ReflectionClass( $hook->getComponent() ) )->getNamespaceName();
                    $namespaceKey = str_replace( '\\', '_', strtolower( $namespace ) );
                    $hookName     = preg_replace( '/[^A-Za-z0-9_]/', '', strtolower( $hook->getHook() ) );
                    $callbackName = preg_replace( '/[^A-Za-z0-9_]/', '', strtolower( $hook->getCallback() ) );
                    $key          = sprintf( 'hooks_%s_%s_%s_%s', $namespaceKey, strtolower( $name ), $hookName, $callbackName );
                    $status       = ( isset( $this->config->options->fab_hooks->$key ) ) ? $this->config->options->fab_hooks->$key : $hook->isStatus(); // Option Exists
                    $status       = ( $status==='true' || $status=='1' ) ? true : false; // Grab option status
                    if ( $status == false && ! $hook->isMandatory() ) {
                        continue; // Check plugin isMandatory
                    }
                    $hook->run();
                }
            }
        }
    }

    /**
     * Get instance
     *
     * @return $this
     */
    public static function getInstance() {
        if ( self::$instance == null ) {
            self::$instance = new Plugin();
        }
        return self::$instance;
    }

    /**
     * Set default config
     *
     * @return void
     */
    public function setDefaultOption(){
        $config = (array) $this->config->options;
        if(empty($config) || !$config){
            $config = $this->Helper->ArrayMergeRecursive(
                (array) $this->config->default,
                (array) $this->config->options
            );
            $this->WP->update_option( 'fab_config', (object) $config );
            $this->config->options = (object) $config;
        }
    }

    /**
     * @return string
     */
    public function getName(): string {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName( string $name ): void {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getSlug() {
        return $this->slug;
    }

    /**
     * @param string $slug
     */
    public function setSlug( $slug ): void {
        $this->slug = $slug;
    }

    /**
     * @return string
     */
    public function getVersion(): string {
        return $this->version;
    }

    /**
     * @param string $version
     */
    public function setVersion( string $version ): void {
        $this->version = $version;
    }

    /**
     * @return bool
     */
    public function isProduction(): bool {
        return $this->production;
    }

    /**
     * @param bool $production
     */
    public function setProduction( bool $production ): void {
        $this->production = $production;
    }

    /**
     * @return array
     */
    public function getEnableHooks(): array {
        return $this->enableHooks;
    }

    /**
     * @param array $enableHooks
     */
    public function setEnableHooks( array $enableHooks ): void {
        $this->enableHooks = $enableHooks;
    }

    /**
     * @return array
     */
    public function getPath(): array {
        return $this->path;
    }

    /**
     * @param array $path
     */
    public function setPath( array $path ): void {
        $this->path = $path;
    }

    /**
     * @return array
     */
    public function getApis() {
        return $this->apis;
    }

    /**
     * @param array $apis
     */
    public function setApis( $apis ) {
        $this->apis = $apis;
    }

    /**
     * @return array
     */
    public function getControllers(): array {
        return $this->controllers;
    }

    /**
     * @param array $controllers
     */
    public function setControllers( array $controllers ): void {
        $this->controllers = $controllers;
    }

    /**
     * @return array
     */
    public function getFeatures() {
        return $this->features;
    }

    /**
     * @param array $features
     */
    public function setFeatures( $features ) {
        $this->features = $features;
    }

    /**
     * @return array
     */
    public function getModels(): array {
        return $this->models;
    }

    /**
     * @param array $models
     */
    public function setModels( array $models ): void {
        $this->models = $models;
    }

    /**
     * @return object
     */
    public function getConfig() {
        return $this->config;
    }

    /**
     * @param object $config
     */
    public function setConfig( $config ): void {
        $this->config = $config;
    }

    /**
     * @return object
     */
    public function getHelper() {
        return $this->Helper;
    }

    /**
     * @param object $Helper
     */
    public function setHelper( $Helper ) {
        $this->Helper = $Helper;
    }

    /**
     * @return object
     */
    public function getForm(): Form
    {
        return $this->Form;
    }

    /**
     * @param object $Form
     */
    public function setForm(Form $Form): void
    {
        $this->Form = $Form;
    }

    /**
     * @return object
     */
    public function getWP(): Wordpress\Helper {
        return $this->WP;
    }

    /**
     * @param object $WP
     */
    public function setWP( Wordpress\Helper $WP ): void {
        $this->WP = $WP;
    }

}