Overview

Namespaces

  • None
  • VSP
    • Core
      • Abstracts
      • Interfaces
      • Traits
        • WC_Compatibility
    • Deprecation
    • Helper
    • Modules
      • Addons
      • Logger

Classes

  • VSP\Ajax
  • VSP\Ajaxer
  • VSP\Base
  • VSP\Cache
  • VSP\Core\Abstracts\Addon
  • VSP\Core\Abstracts\Log_Handler
  • VSP\Core\Abstracts\Plugin_Settings
  • VSP\Core\Instance_Handler
  • VSP\Deprecation\Actions
  • VSP\Deprecation\Filters
  • VSP\Error
  • VSP\Framework
  • VSP\Framework_Admin
  • VSP\Framework_Base
  • VSP\Framework_Modules
  • VSP\Helper
  • VSP\Helper\Price_Calculation
  • VSP\Modules\Addons
  • VSP\Modules\Addons\Admin
  • VSP\Modules\Addons\Core
  • VSP\Modules\Logger
  • VSP\Modules\Logger\File_Handler
  • VSP\Modules\Logger\Levels
  • VSP\Modules\Shortcode
  • VSP\Modules\System_Logs
  • VSP\Modules\System_Tools
  • VSP\Modules\WPOnion
  • VSP\Setup
  • VSP\WC_Compatibility
  • VSP_Framework_Loader

Interfaces

  • VSP\Core\Interfaces\Log_Handler
  • VSP\Core\Interfaces\Logger

Traits

  • VSP\Core\Traits\Array_Helper
  • VSP\Core\Traits\Framework
  • VSP\Core\Traits\String_Helper
  • VSP\Core\Traits\URL
  • VSP\Core\Traits\WC_Compatibility\Product
  • VSP\Core\Traits\WC_Compatibility\Version
  • VSP\Core\Traits\WC_Helper
  • VSP\Core\Traits\WP

Functions

  • vsp_add_wc_required_notice
  • vsp_ajax_action
  • vsp_current_screen
  • vsp_date_format
  • vsp_delete_cache
  • vsp_doing_it_wrong
  • vsp_force_load
  • vsp_force_load_vendors
  • vsp_get_cache
  • vsp_get_cache_defaults
  • vsp_get_file_paths
  • vsp_get_logger
  • vsp_get_time_in_seconds
  • vsp_is_admin
  • vsp_is_ajax
  • vsp_is_cron
  • vsp_is_error
  • vsp_is_frontend
  • vsp_is_heartbeat
  • vsp_is_json
  • vsp_is_request
  • vsp_is_screen
  • vsp_json_last_error
  • vsp_list_files
  • vsp_list_log_files
  • vsp_load_core_assets
  • vsp_load_file
  • vsp_log_msg
  • vsp_logger
  • vsp_make_log_list_tree
  • vsp_maybe_load
  • vsp_placeholder_img
  • vsp_print_log_files_ui
  • vsp_register_assets
  • vsp_register_plugin
  • vsp_send_callback_error
  • vsp_send_callback_success
  • vsp_send_json_callback
  • vsp_set_cache
  • vsp_set_time_limit
  • vsp_slashit
  • vsp_time_format
  • vsp_unslashit
  • vsp_url
  • vsp_validate_required_plugin
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 
<?php

namespace VSP\Modules;

defined( 'ABSPATH' ) || exit;

/**
 * Class VSP_Addons
 *
 * @author Varun Sridharan <varunsridharan23@gmail.com>
 */
class Addons extends Addons\Admin {
    /**
     * @return array
     */
    protected function defaults() {
        return array(
            'base_path'               => '',
            'base_url'                => '',
            'addon_listing_tab_name'  => 'addons',
            'addon_listing_tab_title' => 'Addons',
            'addon_listing_tab_icon'  => 'fa fa-plus',
            'headers'                 => array(),
            'show_category_count'     => true,
            'hook_priority'           => 99,
        );
    }

    /**
     * VSP_Addons constructor.
     *
     * @param array $options .
     */
    public function __construct( $options = array() ) {
        $this->set_args( $options );
        self::$default_addon_cats      = array(
            'all'      => esc_html__( 'All', 'vsp-framework' ),
            'active'   => esc_html__( 'Active', 'vsp-framework' ),
            'inactive' => esc_html__( 'Inactive', 'vsp-framework' ),
            'general'  => esc_html__( 'General', 'vsp-framework' ),
        );
        self::$required_plugins_status = array(
            'notexists' => esc_html__( 'Not Installed', 'vsp-framework' ),
            'exists'    => esc_html__( 'In Active', 'vsp-framework' ),
            'active'    => esc_html__( 'Active', 'vsp-framework' ),
        );
        $this->headers                 = $this->parse_args( $this->option( 'headers' ), $this->default_headers );
        $slug                          = $this->plugin()->slug( 'hook' );
        $hook                          = $this->option( 'hook_priority' );
        $this->active_addons();
        $this->load_active_addons();

        if ( vsp_is_admin() ) {
            $this->add_action( $slug . '/settings/fields', 'link_with_wponion', $hook );
        }

        if ( vsp_is_ajax() ) {
            add_action( $slug . '/addon/ajax/handle/request', array( $this, 'handle_ajax_request' ) );
        }
    }

    /**
     * @param \VSP\Ajax $ajax
     */
    public function handle_ajax_request( $ajax ) {
        $addon  = $ajax->post( 'addon' );
        $action = $ajax->request( 'addon_action' );

        if ( empty( $addon ) ) {
            $ajax->error( esc_html__( 'Invalid Addon', 'vsp-framework' ) );
        }

        if ( ! in_array( $action, array( 'activate', 'deactivate' ), true ) ) {
            $ajax->error( esc_html__( 'Invalid Addon Action', 'vsp-framework' ) );
        }

        switch ( $action ) {
            case 'activate':
                if ( ! $this->is_active( $addon ) ) {
                    $data = $this->search_addon( $addon );

                    if ( ! is_array( $data ) ) {
                        vsp_send_callback_error( swal2_error( esc_html__( 'Addon Not Found', 'vsp-framework' ), esc_html__( 'Selected Addon Not Found. Please Contact The Developer', 'vsp-framework' ) ) );
                    }

                    if ( isset( $data['required_plugins'] ) && is_array( $data['required_plugins'] ) && ! empty( $data['required_plugins'] ) && true !== $data['required_plugins_fulfilled'] ) {
                        $msg = swal2_error( esc_html__( 'Activation Failed', 'vsp-framework' ), esc_html__( 'Addon\'s Requried Plugins Not Active / Installed', 'vsp-framework' ) );
                        vsp_send_callback_error( $msg );
                    }

                    if ( $this->activate_addon( $addon ) ) {
                        vsp_send_callback_success( swal2_success( esc_html__( 'Addon Activated', 'vsp-framework' ) ) );
                    }
                }
                vsp_send_callback_error( swal2_warning( esc_html__( 'Addon Already Active', 'vsp-framework' ) ) );
                break;
            case 'deactivate':
                if ( $this->is_active( $addon ) && $this->deactivate_addon( $addon ) ) {
                    vsp_send_callback_success( swal2_warning( esc_html__( 'Addon De-Activated', 'vsp-framework' ) ) );
                }
                vsp_send_callback_error( swal2_warning( esc_html__( 'Addon Is Not Active', 'vsp-framework' ) ) );
                break;
        }
    }

    /**
     * Loads Active Addons.
     */
    public function load_active_addons() {
        $active_addons       = $this->active_addons();
        $msg                 = esc_html__( 'Following addons are deactivated because some of its required plugins are deactivated / uninstalled', 'vsp-framework' );
        $deactivated_plugins = '';
        if ( ! empty( $active_addons ) ) {
            foreach ( $active_addons as $pathid ) {
                $is_active = $this->is_active( $pathid );
                if ( false !== $is_active ) {
                    $addon_data = $this->search_addon( $pathid );
                    if ( empty( $addon_data ) ) {
                        $deactivated_plugins .= '<li>' . vsp_slashit( basename( $addon_data['addon_path'] ) ) . $addon_data['file'] . '</li>';
                        $this->deactivate_addon( $pathid );
                        continue;
                    }
                    if ( isset( $addon_data['required_plugins'] ) && is_array( $addon_data['required_plugins'] ) && ! empty( $addon_data['required_plugins'] ) && true !== $addon_data['required_plugins_fulfilled'] ) {
                        $deactivated_plugins .= '<li>' . $addon_data['name'] . '</li>';
                        $this->deactivate_addon( $pathid );
                        continue;
                    }
                    $full_path = vsp_slashit( $addon_data['addon_path'] ) . $addon_data['file'];
                    vsp_load_file( $full_path );
                }
            }
        }
        if ( ! empty( $deactivated_plugins ) ) {
            $title = '<strong>' . $this->plugin()
                    ->name() . '</strong>' . esc_html__( ' Has Deactivated Some of its addons', 'vsp-framework' );
            $msg   = $msg . '<ul>' . $deactivated_plugins . '</ul>';
            $msg   .= '<p><button class="button button-secondary wpo-stick-dismiss">' . esc_html__( 'I Understand. Will Fix It', 'vsp-framework' ) . '</button></p>';

            wponion_error_admin_notice( $msg, $title, array( 'large' => true ) )->set_sticky( true );
        }
    }

    /**
     * Returns All Active Addons For current plugin
     *
     * @return array|false
     */
    public function active_addons() {
        if ( false === $this->active_addons ) {
            $this->active_addons = wponion_cast_array( get_option( $this->plugin()->slug( 'db' ) . '_active_addons' ) );
        }
        return $this->active_addons;
    }

    /**
     * Handles Ajax Request Params For Addons
     *
     * @param string $request .
     * @param string $msg .
     *
     * @return bool
     */
    public function handle_ajax_params( $request, $msg ) {
        if ( isset( $_POST[ $request ] ) ) {
            return $_POST[ $request ];
        }
        wp_send_json_error( $msg );
        return false;
    }

    /**
     * Updates Active Addons in db
     *
     * @param array $addons .
     *
     * @return array
     */
    public function update_active_addons( $addons ) {
        update_option( $this->plugin()->slug( 'db' ) . '_active_addons', $addons );
        $this->active_addons = $addons;
        return $this->active_addons;
    }

    /**
     * Activates Selected Addon.
     *
     * @param string $addon_uid Addons MD5 Unique ID
     *
     * @return bool
     */
    public function activate_addon( $addon_uid = '' ) {
        $active_addons = $this->active_addons();
        if ( ! in_array( $addon_uid, $active_addons, true ) ) {
            $active_addons[] = $addon_uid;
            $this->update_active_addons( $active_addons );
            return true;
        }
        return false;
    }

    /**
     * Deactivates Selected Addon
     *
     * @param string $addon_uid Addons MD5 Unique ID
     *
     * @return bool
     */
    public function deactivate_addon( $addon_uid = '' ) {
        $active_addons = $this->active_addons();
        if ( in_array( $addon_uid, $active_addons, true ) ) {
            foreach ( $active_addons as $k => $id ) {
                if ( $id === $addon_uid ) {
                    unset( $active_addons[ $k ] );
                }
            }
            $this->update_active_addons( $active_addons );
            return true;
        }
        return false;
    }

    /**
     * Checks if given addon is active
     *
     * @param string $addon_uid Addons MD5 Unique ID
     *
     * @return bool|mixed
     */
    public function is_active( $addon_uid = '' ) {
        $addons = $this->active_addons();
        return ( in_array( $addon_uid, $addons, true ) );
    }
}
API documentation generated by ApiGen