# media-cloud-sync/1.0.3/includes/admin.php

Media Cloud Sync, version 1.0.3. 419 lines.

- Page: https://pluginprobe.com/plugins/media-cloud-sync/1.0.3/code/includes/admin.php
- Raw: https://pluginprobe.com/plugins/media-cloud-sync/1.0.3/raw/includes/admin.php
- Modified: 2024-11-17T20:11:40+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/media-cloud-sync/1.0.3/code/includes/admin.php#L10-L20`.

```php
<?php
namespace Dudlewebs\WPMCS;

defined('ABSPATH') || exit;

class Admin {
    private static $instance = null;
    private $assets_url;
    private $version;
    private $token;
    private $script_suffix;

    protected $hook_suffix = [];
    /**
     * Admin constructor.
     * @since 1.0.0
     */
    public function __construct() {
        $this->assets_url       = WPMCS_ASSETS_URL;
        $this->version          = WPMCS_VERSION;
        $this->token            = WPMCS_TOKEN;
        $this->script_suffix    = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';

        $plugin = plugin_basename(WPMCS_FILE);

        // Create file directory on action do
        add_action( $this->token.'_create_plugin_dir', [ $this, 'create_plugin_dir' ] );

        // add action links to link to link list display on the plugins page.
        add_filter("plugin_action_links_$plugin", [$this, 'plugin_action_links']);

        // add our custom CSS classes to <body>
		add_filter( 'admin_body_class', [ $this, 'admin_body_class' ] );
        // Admin Init
        add_action('admin_init', [$this, 'adminInit']);

        add_action('admin_menu', [$this, 'add_menu'], 10);

        add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts'], 10, 1);
        add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_styles'], 10, 1);
        // Load media scripts
        add_action( 'load-upload.php', [ $this, 'load_media_assets' ], 11 );

        // Plugin Deactivation Survey
        add_action('admin_footer', array($this, 'wpmcs_deactivation_form'));
    }

    /**
     * Method that is used on plugin initialization time
     * @since 1.0.0
     */
    public function adminInit() {
        if(get_option( $this->token.'_db_version' ) !== WPMCS_DB_VERSION) {
            $database = Db::instance();
            $database->do_database_upgrade();
        }

        if (get_option($this->token.'_do_activation_redirect', false) && !Utils::get_service()) {
            delete_option($this->token.'_do_activation_redirect');
            wp_redirect(admin_url('admin.php?page=' . $this->token . '-admin-ui#/configure'));
        } else {
            delete_option($this->token.'_do_activation_redirect');
        }
    }

    /**
     * Installation. Runs on activation.
     *
     * @access  public
     * @return  void
     * @since   1.0.0
     */
    public function install(){
        $database = Db::instance();

        $database->create_table();

        // Redirection on activation
        add_option($this->token.'_do_activation_redirect', true);

        //Protect directories
        $this->_protect_upload_dir();
    }

    /**
     * Create plugin specific upload directory
     * @since 1.0.0
     */
    public function create_plugin_dir() {
        //Protect directories
        $this->_protect_upload_dir();
    }

    /**
     * Protect Directory from external access.
     *
     * @access  private
     * @return  void
     * @since   1.0.0
     */
    private function _protect_upload_dir(){
        $upload_dir = wp_upload_dir();

        $files = array(
            array(
                'base' => $upload_dir['basedir'] . '/' . Schema::getConstant('UPLOADS'),
                'file' => '.htaccess',
                'content' => 'Options -Indexes' . "\n"
                    . '<Files *.php>' . "\n"
                    . 'deny from all' . "\n"
                    . '</Files>'
            ),
            array(
                'base' => $upload_dir['basedir'] . '/' . Schema::getConstant('UPLOADS'),
                'file' => 'index.php',
                'content' => '<?php ' . "\n"
                    . '// Silence is golden.'
            )
        );

        foreach ($files as $file) {
            if ((wp_mkdir_p($file['base'])) && (!file_exists(trailingslashit($file['base']) . $file['file']))  // If file not exist
            ) {
                if ($file_handle = @fopen(trailingslashit($file['base']) . $file['file'], 'w')) {
                    fwrite($file_handle, $file['content']);
                    fclose($file_handle);
                }
            }
        }
    }

    /**
     * Deactivation hook
     */
    public function deactivation(){
    }

    /**
     * Load admin Javascript.
     * @access  public
     * @return  void
     * @since   1.0.0
     */
    public function admin_enqueue_scripts($hook = '') {
        if (!isset($this->hook_suffix) || empty($this->hook_suffix)) {
            return;
        }

        $screen = get_current_screen();

        // deactivate form js
        if ($screen->id == 'plugins') { 
            wp_enqueue_script($this->token . '-deactivate-form', esc_url($this->assets_url) . 'js/deactivate.js', array('jquery'), $this->version, true);
        }


        if (in_array($screen->id, $this->hook_suffix, true)) {
            // Enqueue WordPress media scripts.
            if (!did_action('wp_enqueue_media')) {
                wp_enqueue_media();
            }

            // Enqueue custom backend script.
            wp_enqueue_script($this->token . '-backend', esc_url($this->assets_url) . 'js/backend.js', array('wp-i18n'), $this->version, true);

            // Localize a script.
            wp_localize_script(
                $this->token . '-backend',
                $this->token . '_object',
                array(
                    'api_nonce' => wp_create_nonce('wp_rest'),
                    'root' => rest_url($this->token . '/v1/'),
                    'assets_url' => $this->assets_url,
                )
            );
        }
    }



    /**
     * Load admin CSS.
     * @access  public
     * @return  void
     * @since   1.0.0
     */
    public function admin_enqueue_styles($hook = '') {
        if ( ! isset($this->hook_suffix) || empty($this->hook_suffix)) {
            return;
        } 
        $screen = get_current_screen();
        if (in_array($screen->id, $this->hook_suffix)) {
            wp_register_style($this->token.'-backend',
                esc_url($this->assets_url).'css/backend.css?nocache='.rand(0, 10000), array(), $this->version);
            wp_enqueue_style($this->token.'-backend');
        }

        if ($screen->id == 'plugins') { 
            wp_enqueue_style($this->token . '-deactivate-form', esc_url($this->assets_url) . 'css/deactivate.css', array(), $this->version);
        }
    }

    /**
     * Show action links on the plugin screen.
     *
     * @param mixed $links Plugin Action links.
     *
     * @return array
     */
    public function plugin_action_links($links){
        $action_links = array(
            'getstarted' => '<a href="' . admin_url('admin.php?page=' . $this->token . '-admin-ui#/configure') . '">' . esc_html__('Get Started', 'media-cloud-sync') . '</a>',
            'settings' => '<a href="' . admin_url('admin.php?page=' . $this->token . '-admin-ui/') . '">' . esc_html__('Settings', 'media-cloud-sync') . '</a>',
        );

        return array_merge($action_links, $links);
    }




    /**
     * Add Admin Menu
     */
    public function add_menu() {
        $this->hook_suffix[] = add_menu_page(
            esc_html__('Media Cloud Sync', 'media-cloud-sync'),
            esc_html__('Media Cloud Sync', 'media-cloud-sync'),
            'manage_options',
            $this->token.'-admin-ui',
            array($this, 'adminUi'),
            'data:image/svg+xml;base64,P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ZGVmcz4KPC9zdmc+Cg==',
            25
        );
     
        $this->hook_suffix[] = add_submenu_page(
            $this->token . '-admin-ui', 
            esc_html__('Dashboard', 'media-cloud-sync'), 
            esc_html__('Dashboard', 'media-cloud-sync'), 
            'manage_options', 
            $this->token . '-admin-ui' ,
            array($this, 'adminUi')
        );

        if(Utils::is_service_enabled()) {
            $this->hook_suffix[] = add_submenu_page(
                $this->token . '-admin-ui', 
                esc_html__('Settings', 'media-cloud-sync'), 
                esc_html__('Settings', 'media-cloud-sync'), 
                'manage_options', 
                $this->token . '-admin-ui#/settings' ,
                array($this, 'adminUi')
            );
        } else {
            $this->hook_suffix[] = add_submenu_page(
                $this->token . '-admin-ui', 
                esc_html__('Configure', 'media-cloud-sync'), 
                esc_html__('Configure', 'media-cloud-sync'), 
                'manage_options', 
                $this->token . '-admin-ui#/configure' ,
                array($this, 'adminUi')
            );
        }
    }

    /**
     * Calling view function for admin page components
     */
    public function adminUi() {
        echo wp_kses_post(sprintf(
            '<div id="%s_ui_root">
                <div class="%s_loader">
                    <h1>%s</h1>
                    <p>%s</p>
                </div>
            </div>', 
            $this->token,
            $this->token,
            esc_html__('Media Cloud Sync By Dudlewebs','media-cloud-sync'),
            esc_html__('Plugin is loading Please wait for a while..', 'media-cloud-sync')
        ));
       
    }


    /**
	 * Load the media assets
	 */
	public function load_media_assets() {
        /** CSS */
        wp_enqueue_style($this->token . '-media', esc_url($this->assets_url) . 'css/media.css', array(), $this->version);
        
        /** JS */
        wp_enqueue_script(
            $this->token . '-media', 
            esc_url($this->assets_url) . 'js/media.js', 
            array(
                'jquery',
                'media-views',
                'media-grid',
                'wp-util'
            ),
            $this->version, 
            true
        );

        // Localize a script.
        wp_localize_script(
            $this->token . '-media',
            $this->token . '_media_object',
            array(
                'file_details_nonce'    => wp_create_nonce('get_media_provider_details'),
                'admin_ajax_url'        => admin_url('admin-ajax.php'),
                'strings'               => array(
                    'provider'          => esc_html__("Provider: ", "media-cloud-sync"),
                    'region'            => esc_html__("Region: ", "media-cloud-sync"),
                    'access'            => esc_html__("Access: ", "media-cloud-sync"),
                    'access_private'    => esc_html__("Private", "media-cloud-sync"),
                    'access_public'     => esc_html__("Public", "media-cloud-sync"),
                )      
            )
        );
    }


    /**
     * Deactivation form
     * @since 1.0.2
     */
    public function wpmcs_deactivation_form() {
        $currentScreen = get_current_screen();
        $screenID = $currentScreen->id;
        if ($screenID == 'plugins') { 
            ?>
            <div id="dw-wpmcs-survey-form-wrap">
                <div id="dw-wpmcs-survey-form">
                    <h2>We Value Your Feedback</h2>
                    <p>We're sorry to see you go! Please let us know why you are deactivating this plugin. Your feedback is anonymous and will help us improve.</p>
                    <form method="POST">
                        <input name="plugin_name" type="hidden" value="<?= $this->token ?>" required>
                        <input name="website" type="hidden" value="<?= get_site_url(); ?>" required>
                        <input name="title" type="hidden" value="<?= get_bloginfo('name'); ?>" required>
                        <input name="version" type="hidden" value="<?= $this->version; ?>" required>
                        
                        <div class="dw-wpmcs-reason">
                            <label><input type="radio" name="reason" value="I\'m only deactivating temporarily"> I'm only deactivating temporarily</label>
                            <label><input type="radio" name="reason" value="I no longer need the plugin"> I no longer need the plugin</label>
                            <label><input type="radio" name="reason" value="I only needed the plugin for a short period"> I only needed the plugin for a short period</label>
                            <label><input type="radio" name="reason" value="I found a better plugin"> I found a better plugin</label>
                            <label><input type="radio" name="reason" value="Upgrading to PRO version"> Upgrading to PRO version</label>
                            <label><input type="radio" name="reason" value="Plugin doesn\'t meet my requirements"> Plugin doesn't meet my requirements</label>
                            <label><input type="radio" name="reason" value="Plugin broke my site"> Plugin broke my site</label>
                            <label><input type="radio" name="reason" value="Plugin suddenly stopped working"> Plugin suddenly stopped working</label>
                            <label><input type="radio" name="reason" value="I found a bug"> I found a bug</label>
                            <label><input type="radio" name="reason" value="Other"> Other</label>
                        </div>
                        
                        <div class="dw-wpmcs-comments" style="display:none;">
                            <textarea name="comments" placeholder="Please specify" rows="3"></textarea>
                            <p>Need help? <a href="https://dudlewebs.com/" target="_blank">Use live chat support</a></p>
                        </div>
                        
                        <p id="dw-wpmcs-error" class="dw-wpmcs-error"></p>
                        <div class="dw-wpmcs-actions">
                            <button type="submit" class="dw-button dw-primary" id="dw-wpmcs-deactivate">Submit & Deactivate</button>
                            <button type="button" class="dw-button dw-secondary" id="dw-wpmcs-cancel">Cancel</button>
                            <button type="button" class="dw-button dw-skip" id="dw-wpmcs-skip">Skip & Deactivate</button>
                        </div>
                    </form>
                </div>
            </div>
        <?php }
    }



    /**
	 * Add custom classes to the HTML body tag
	 *
	 * @param string $classes
	 *
	 * @return string
	 */
	public function admin_body_class( $classes ) {
		if ( ! $classes ) {
			$classes = array();
		} else {
			$classes = explode( ' ', $classes );
		}
		$classes[] = $this->token.'_page';
		/**
         *  Recommended way to target WP 3.8+
         *  http://make.wordpress.org/ui/2013/11/19/targeting-the-new-dashboard-design-in-a-post-mp6-world/
         * 
         */
		if ( version_compare( $GLOBALS['wp_version'], '3.8-alpha', '>' ) ) {
			if ( ! in_array( 'mp6', $classes ) ) {
				$classes[] = 'mp6';
			}
		}
		return implode( ' ', $classes );
	}
    

    /**
     * Ensures only one instance of Class is loaded or can be loaded.
     *
     * @return Main Class instance
     * @since 1.0.0
     * @static
     */
    public static function instance(){
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

}
```
