File: /home/zeusxp5/chuair.org/wp-content/themes/lifttruck/inc/panel/PanelClass.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
include_once ABSPATH . 'wp-admin/includes/plugin.php';
class PanelClass {
var $pageTitle;
var $menuTitle;
var $capability;
var $menuSlug;
var $sectionName = array();
var $sectionParams = array();
var $plugins = array();
var $demos = array();
var $namespace = 'wd/v2';
public function __construct( $pageTitle, $menuTitle, $capability, $menuSlug, $plugins, $demos ) {
$this->pageTitle = $pageTitle;
$this->menuSlug = $menuSlug;
$this->capability = $capability;
$this->menuTitle = $menuTitle;
$this->plugins = $plugins;
$this->demos = $demos;
$this->init();
}
public function init() {
add_action( 'wp_loaded', [ $this, 'redirect_to_panel_page' ], 1 );
add_action( 'admin_menu', [ $this, 'setup_panel_page' ], 1 );
add_action( 'admin_enqueue_scripts', [ $this, 'panel_script_enqueue' ] );
add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
add_action( 'admin_footer', [ $this, 'render_modal' ] );
}
public function panel_script_enqueue( $hook_suffix ) {
if ( is_admin() ) {
wp_enqueue_style( 'wd-admin-style', get_template_directory_uri() . '/inc/css/style.css', array(), '20120208', false );
}
$screen = get_current_screen();
if ( is_admin() && ( false !== strpos( $hook_suffix, wp_get_theme()->get( 'TextDomain' ) . "_theme_options" ) || 'themes' == $screen->id ) ) {
wp_enqueue_media();
wp_enqueue_style( 'wd-style' );
wp_enqueue_style( 'wp-components' );
wp_register_style( 'wd-style', get_template_directory_uri() . '/inc/css/onboarding.css', array(), '20120208', false );
wp_enqueue_style( 'wd-panel-style', get_template_directory_uri() . "/inc/panel/build/style-index.css" );
$script_asset = include get_parent_theme_file_path( '/inc/panel/build/index.asset.php' );
wp_enqueue_script( 'webdevia-onboarding-settings', get_template_directory_uri() . '/inc/panel/build/index.js', $script_asset['dependencies'], $script_asset['version'], true );
wp_set_script_translations(
'webdevia-onboarding-settings',
TEXT_DOMAIN
);
$devia_settings = get_option( TEXT_DOMAIN . '_options_array' );
// set to empty array if the function get_option( TEXT_DOMAIN . '_options_array' ) does not return an array
$devia_settings = is_array( $devia_settings ) ? $devia_settings : theme_base_class::initialize_options();
if ( get_option( 'site_logo' ) ) {
$logo_id = is_array(get_option( 'site_logo' )) ? get_option( 'site_logo' )[0] : get_option( 'site_logo' );
$logo_link = wp_get_attachment_image_url( $logo_id, 'full', true );
$devia_settings["logo_link"] = [ get_option( 'site_logo' ), $logo_link ];
}
if ( get_option( 'site_icon' ) ) {
$favicon_id = is_array(get_option( 'site_icon' )) ? get_option( 'site_icon' )[0] : get_option( 'site_icon' );
$favicon_link = wp_get_attachment_image_url( $favicon_id, 'full', true );
$devia_settings["favicon_link"] = [ get_option( 'site_icon' ), $favicon_link ];
}
$devia_settings["fields"] = json_encode( include( get_template_directory() . '/inc/config/panel-options-fields.php') );
$args = array(
'screen' => in_array( $screen->id, [ 'themes' ] ) ? 'modal' : '',
'name' => THEME_NAME,
'version' => THEME_VERSION,
'text_domain' => TEXT_DOMAIN,
'theme_params' => include( get_template_directory() . '/inc/config/panel-theme-description.php'),
'admin_dash_link' => esc_url( admin_url() ),
'dashboard_link' => esc_url( admin_url() ) . '/admin.php?page=' . TEXT_DOMAIN . '_theme_options',
'home_link' => esc_url( home_url() ),
'rest_url' => get_rest_url(),
'logo' => esc_url( home_url() ) . '/assets/devia-logo.svg',
'onboarding_image' => get_template_directory_uri() . '/inc/images/onboarding_img_1.webp',
'onboarding_complete' => false,
'nonce' => wp_create_nonce( 'wp_rest' ),
'is_theme_active' => $this->wd_check_purchase_code(),
'theme_options' => $devia_settings
);
wp_localize_script( 'webdevia-onboarding-settings', 'devia_options', $args );
// wp_localize_script( 'webdevia-onboarding-settings', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
}
public function setup_panel_page() {
global $title;
// Check if $title is empty and set a default value
if (empty($title)) {
$title = $this->pageTitle;
}
add_menu_page( $this->pageTitle, $this->menuTitle, $this->capability, $this->menuSlug, [ $this, 'theme_welcome_page' ], 'dashicons-schedule', 3 );
add_submenu_page( $this->menuSlug, "Welcome Page", "Welcome Page", $this->capability, 'theme_welcome', [ $this, 'theme_panel_welcome_page' ], 1 );
$purchaseCode = get_user_meta( get_current_user_id(), 'wd_purchase', true);
if ($purchaseCode && $this->purchase_code_verification( $purchaseCode ) ) {
add_submenu_page( $this->menuSlug, 'Import Demo Content', 'Import Demo Content', $this->capability, 'theme_demo_content', [ $this, 'theme_demo_import' ], 3);
add_submenu_page( $this->menuSlug, "Theme Options", "Theme Options", $this->capability, 'theme_panel_options', [ $this, 'theme_options_page' ], 4 );
} else {
add_submenu_page( $this->menuSlug, 'Theme Activation', 'Theme Activation', $this->capability, 'theme_activation', [ $this, 'theme_activation' ], 2);
}
add_submenu_page( $this->menuSlug, "Documentation", "Documentation", $this->capability, 'theme_panel_documentation', [ $this, 'theme_documentation_page' ], 5 );
// Remove the default first submenu page
global $submenu;
unset($submenu[$this->menuSlug][0]);
}
/**
*
*/
public function render_modal() {
$currentScreen = get_current_screen();
if ( $currentScreen->id === "themes" ) { ?>
<div id="devia-modal"></div>
<style>
@keyframes DeviaFadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.devia-modal-background {
background: rgba(93, 93, 111, 0.7);
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 9991;
animation: DeviaFadeIn .5s;
}
.devia-modal-content {
background: white;
padding: 50px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 600px;
box-shadow: 0 3px 10px rgb(0, 0, 0, 0.2);
z-index: 99;
border-radius: 3px;
}
.devia-modal-close {
background: none;
border: none;
padding: 0;
position: absolute;
right: 20px;
top: 20px;
}
.devia-modal-close:hover {
cursor: pointer;
opacity: .6;
}
.devia-modal-content img {
max-width: 300px;
margin: 0 auto 35px auto;
display: block;
}
.devia-modal-content h2 {
text-align: center;
font-size: 2.2em;
}
.devia-modal-content p {
margin: 25px auto;
font-size: 16px;
text-align: center;
}
.devia-modal-content .devia-modal-inner button {
padding: 15px 20px;
transition: 0.3s ease;
background: #3858e9;
color: white;
border: none;
cursor: pointer;
border-radius: 2px;
font-size: 16px;
}
.devia-modal-content .devia-modal-inner button:hover {
background: #2145e6;
}
.devia-modal-content button.devia-modal-skip {
background: none;
color: #3c434a;
}
.devia-modal-content button.devia-modal-skip:hover {
text-decoration: underline;
background: none;
}
</style>
<?php
}
}
/**
*
*/
public function theme_welcome_page() {
?>
<div id="devia-welcome">
<!--============================================================
Panel here
=================================================-->
</div>
<?php
}
public function redirect_to_panel_page() {
$current_page = isset($_GET['page']) ? $_GET['page'] : false;
if ( is_admin() && $current_page) {
$theme_panel_pages_map = [
'theme_welcome' => '',
'theme_demo_content' => '#/demo-import',
'theme_activation' => '#/theme-activation',
'theme_panel_options' => '#/theme-options',
'theme_panel_documentation' => '#/documentation'
];
foreach ($theme_panel_pages_map as $key => $value) {
if ($current_page == $key) {
wp_redirect(admin_url() . "?page=$this->menuSlug" . $value);
exit;
}
}
}
}
public function theme_panel_welcome_page() {
wp_redirect( admin_url() . "?page=$this->menuSlug" );
exit;
}
public function theme_activation() {
wp_redirect( admin_url() . "?page=$this->menuSlug#/theme-activation" );
exit;
}
public function theme_demo_import() {
wp_redirect( admin_url() . "?page=$this->menuSlug#/demo-import" );
exit;
}
public function theme_options_page() {
wp_redirect( admin_url() . "?page=$this->menuSlug#/theme-options" );
exit;
}
public function theme_documentation_page() {
wp_redirect( admin_url() . "?page=$this->menuSlug#/documentation" );
exit;
}
public function rest_api_init() {
register_rest_route( 'devia/v1', '/skip-onboarding', array(
'methods' => 'POST',
'callback' => [ $this, 'skip_onboarding' ],
'permission_callback' => function () {
return true; //current_user_can( 'manage_options' );
},
) );
register_rest_route( 'devia/v1', '/settings', array(
'methods' => 'GET',
'callback' => [ $this, 'get_settings' ],
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
) );
register_rest_route( 'devia/v1', '/settings', array(
'methods' => 'POST',
'callback' => [ $this, 'save_settings' ],
'permission_callback' => function () {
return current_user_can( 'manage_options' );
}
) );
register_rest_route( $this->namespace, '/demos-plugin', [
'methods' => 'POST',
'callback' => [ $this, 'get_demos_via_rest' ],
'permission_callback' => function () {
return current_user_can( 'activate_plugins' );
}
] );
register_rest_route( $this->namespace, 'install-plugin', [
'methods' => 'POST',
'callback' => [ $this, 'wd_install_plugin' ],
'permission_callback' => function () {
return current_user_can( 'install_plugins' );
}
] );
register_rest_route( $this->namespace, 'is-plugin-active', [
'methods' => 'POST',
'callback' => [ $this, 'wd_is_the_plugin_active' ],
'permission_callback' => function () {
return current_user_can( 'update_plugins' );
}
] );
register_rest_route( $this->namespace, 'activate-plugin', [
'methods' => 'POST',
'callback' => [ $this, 'wd_activate_plugin' ],
'permission_callback' => function () {
return current_user_can( 'activate_plugins' );
}
] );
register_rest_route( $this->namespace, 'check-purchase-code', [
'methods' => 'GET',
'callback' => [ $this, 'wd_check_purchase_code' ],
'permission_callback' => function () {
return true;
}
] );
register_rest_route( $this->namespace, 'save-purchase-code', [
'methods' => 'POST',
'callback' => [ $this, 'wd_save_purchase_code' ],
'permission_callback' => function () {
return true;
}
] );
register_rest_route( $this->namespace, 'wd-get-sys-requirements', [
'methods' => 'GET',
'callback' => [ $this, 'wd_get_system_requirements' ],
'permission_callback' => function () {
return true;
}
] );
}
public function skip_onboarding( $request ) {
if ( $request->get_params() ) {
$initial_options = theme_base_class::initialize_options();
$options = (array) get_option( TEXT_DOMAIN . '_options_array', $initial_options );
$options['skip_onboarding'] = true;
update_option( TEXT_DOMAIN . '_options_array', $options );
return json_encode( [ "status" => 200, "message" => "Ok" ] );
}
return json_encode( [ "status" => 400, "message" => "There was a problem skipping the onboarding." ] );
}
public function get_settings() {
$options = theme_base_class::initialize_options();
return new WP_REST_Response( $options, 200 );
}
public function save_settings( $req ) {
$string = json_decode( $req->get_body() );
// $string = $req->get_body() ;
$option = array();
foreach ( $string as $key => $value ) {
$option[ $key ] = $value;
}
update_option( TEXT_DOMAIN . '_options_array', $option );
update_option( 'site_logo', $option['logo_link'][0] );
update_option( 'site_icon', $option['favicon_link'][0] ); // var_dump( $option );
return rest_ensure_response( "success" );
}
public function wd_get_plugin_status( $slug ) {
$plugin = $this->plugins[$slug];
$plugin_filename = $this->wd_is_plugin_installed( $slug );
$status = $plugin_filename ? 1 : 0; // Default status if plugin is installed
if ( $plugin_filename ) {
$update_info = get_site_transient( 'update_plugins' );
$needs_update = isset( $update_info->response[ $plugin_filename ] );
$status = $needs_update ? 3 : 1; // Update needed or not
if ( ! class_exists( $plugin['mainClass'] ) ) {
$status = 2; // Plugin installed but class not found
}
}
return [
'name' => $plugin['name'],
'slug' => $slug,
'mainClass' => $plugin['mainClass'],
'status' => $status,
'url' => isset( $plugin['source'] ) ? $plugin['source'] : $this->wd_get_plugin_source_by_slug( $slug )
];
}
public function wd_is_plugin_installed( $plugin_slug ) {
$installed_plugins = get_plugins();
foreach ( $installed_plugins as $file => $plugin_data ) {
$parts = explode( '/', $file );
$folder_name = $parts[0];
if ( $folder_name === $plugin_slug ) {
return $file;
}
}
return false;
}
public function wd_get_plugin_source_by_slug( $slug ) {
$url = "https://api.wordpress.org/plugins/info/1.0/$slug.json";
$transient_key = "wd_plugin_source_" . $slug;
$transient = get_transient( $transient_key );
if ( false !== $transient ) {
return $transient;
}
$response = wp_safe_remote_get( $url );
if ( is_wp_error( $response ) ) {
return false;
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( isset( $data['download_link'] ) ) {
set_transient( $transient_key, $data['download_link'], DAY_IN_SECONDS );
return $data['download_link'];
}
return false;
}
public function wd_install_plugin( WP_REST_Request $request ) {
$plugin_url = $request->get_param( 'plugin_url' );
if ( empty( $plugin_url ) ) {
return new WP_REST_Response( array( 'message' => 'Plugin URL is required.' ), 400 );
}
$response = wp_safe_remote_get( $plugin_url, [ 'timeout' => 60 ] );
if ( is_wp_error( $response ) ) {
return new WP_REST_Response( array( 'message' => 'Error downloading the plugin.' ), 500 );
}
// $plugin_file = ABSPATH . 'wp-content/plugins/plugin-tmp.zip';
$zip = wp_upload_bits( 'plugin-tmp.zip', null, wp_remote_retrieve_body( $response ) );
if ( ! empty( $zip['error'] ) ) {
return new WP_REST_Response( array( 'message' => 'Error uploading the plugin.' ), 500 );
}
// Unzip the plugin
$plugin_dir = WP_PLUGIN_DIR;
$zip_file = $zip['file'];
$zip = new ZipArchive;
if ( $zip->open( $zip_file ) === true ) {
$zip->extractTo( $plugin_dir );
$zip->close();
} else {
return new WP_REST_Response( array( 'message' => 'Error unzipping the plugin.' ), 500 );
}
// Success
return new WP_REST_Response( array( 'message' => 'Plugin downloaded and installed successfully.' ), 200 );
}
public function wd_is_the_plugin_active( WP_REST_Request $request ) {
$mainClass = $request->get_param( 'mainClass' );
if ( class_exists( $mainClass ) ) {
return new WP_REST_Response( [ 'active' => true ], 200 );
}
return new WP_REST_Response( [ 'active' => false ], 200 );
}
public function wd_activate_plugin( WP_REST_Request $request ) {
$slug = $request->get_param( 'slug' );
if ( $file_name = $this->wd_is_plugin_installed( $slug ) ) {
$result = activate_plugin( $file_name );
if(function_exists("disable_plugin_redirect")) {
disable_plugin_redirect();
}
}
if ( is_wp_error( $result ) ) {
return new WP_REST_Response( array( 'message' => 'Error activating the plugin.' ), 500 );
}
// Success
return new WP_REST_Response( array( 'message' => 'The plugin has been activated successfully.' ), 200 );
}
public function wd_check_purchase_code() {
$user_id = get_current_user_id();
if ( $user_id === 0 ) {
return new WP_REST_Response( [ 'message' => 'User not logged in' ], 400 );
}
$purchase = json_decode( get_user_meta( $user_id, 'wd_purchase', true ) );
if ( ! $this->purchase_code_verification( $purchase ) ) {
return new WP_REST_Response( [ 'active' => false ], 200 );
}
// Success
return new WP_REST_Response( [
'active' => true,
'name' => sanitize_text_field( $purchase->name ),
'email' => sanitize_text_field( $purchase->email ),
'code' => sanitize_text_field( $purchase->code ),
'date' => sanitize_text_field( $purchase->date )
], 200 );
}
public function wd_save_purchase_code( WP_REST_Request $request ) {
if ( empty( $request->get_param( 'purchase_code' ) ) ) {
return new WP_REST_Response( [ 'message' => 'Purchase code is required.' ], 400 );
}
$user_id = get_current_user_id();
if ( $user_id === 0 ) {
return new WP_REST_Response( [ 'message' => 'User not logged in' ], 400 );
}
$purchase = [
'name' => sanitize_text_field( $request->get_param( 'purchase_name' ) ),
'email' => sanitize_text_field( $request->get_param( 'purchase_email' ) ),
'code' => sanitize_text_field( $request->get_param( 'purchase_code' ) ),
'date' => date( 'Y-m-d H:i:s' )
];
if ( $this->purchase_code_verification( $purchase ) ) {
update_user_meta( $user_id, 'wd_purchase', json_encode( $purchase ) );
} else {
return new WP_REST_Response( [ 'message' => 'Purchase code verification error.' ], 400 );
}
// Success
return new WP_REST_Response( [ 'message' => 'Theme activated successfully.' ], 200 );
}
public function purchase_code_verification( $purchase ) {
if ( empty( $purchase ) ) {
return false;
}
//TODO: Add logic for the purchaseCode verification
return true;
}
public function wd_get_system_requirements() {
global $wpdb;
try {
$dir = wp_upload_dir()['basedir'];
$ml = ini_get( 'memory_limit' );
$mlb = wp_convert_hr_to_bytes( $ml );
$umf = ini_get( 'upload_max_filesize' );
$umfb = wp_convert_hr_to_bytes( $umf );
$pms = ini_get( 'post_max_size' );
$pmsb = wp_convert_hr_to_bytes( $pms );
$map = $wpdb->get_row( "SHOW VARIABLES LIKE 'max_allowed_packet';" );
$map = isset( $map->Value ) ? $map->Value : 0;
$mlg = ( $mlb >= 268435456 ) ? true : false;
$umfg = ( $umfb >= 33554432 ) ? true : false;
$pmsg = ( $pmsb >= 33554432 ) ? true : false;
$mapg = ( $map >= 16777216 ) ? true : false;
return new WP_REST_Response( array(
'memory_limit' => array(
'has' => size_format( $mlb ),
'min' => size_format( 268435456 ),
'good' => $mlg
),
'upload_max_filesize' => array(
'has' => size_format( $umfb ),
'min' => size_format( 33554432 ),
'good' => $umfg
),
'post_max_size' => array(
'has' => size_format( $pmsb ),
'min' => size_format( 33554432 ),
'good' => $pmsg
),
'max_allowed_packet' => array(
'has' => size_format( $map ),
'min' => size_format( 16777216 ),
'good' => $mapg
),
'allow_url_fopen' => !!ini_get('allow_url_fopen'),
'upload_folder_writable' => wp_is_writable( $dir ),
'zlib_enabled' => function_exists( 'gzcompress' ) && function_exists( 'gzuncompress' ),
'object_library_writable' => wp_image_editor_supports( array( 'methods' => array( 'resize', 'save' ) ) ),
// 'server_connect' => get_option('webdevia-connection', false),
), 200 );
} catch (\Exception $e) {
return new WP_REST_Response( [ 'message' => "Error: " . $e->getMessage() ], 500 );
}
}
private function get_demos() {
$demos = $this->demos;
foreach ($demos as &$demo) {
foreach ($demo['plugins'] as &$slug) { // Use &$slug to modify the array directly
$slug = $this->wd_get_plugin_status($slug);
}
}
return $demos;
}
function get_demos_via_rest() {
$demos = $this->get_demos(); // Replace with your function to fetch demos
return rest_ensure_response($demos);
}
}
add_action('after_setup_theme', 'panel_dashboard');
function panel_dashboard() {
$demos = include( get_template_directory() . '/inc/config/panel-demos.php');
$plugins = include( get_template_directory() . '/inc/config/panel-plugins.php');
$panel = new PanelClass( THEME_NAME . ' Theme Options', THEME_NAME . ' Theme', 'edit_theme_options', TEXT_DOMAIN . '_theme_options', $plugins, $demos );
}