balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

Admin.php (2890B)


      1 <?php
      2 /**
      3  * Admin.
      4  */
      5 
      6 namespace Extendify\ExtendifySdk;
      7 
      8 use Extendify\ExtendifySdk\App;
      9 use Extendify\ExtendifySdk\User;
     10 
     11 /**
     12  * This class handles any file loading for the admin area.
     13  */
     14 class Admin
     15 {
     16 
     17     /**
     18      * The instance
     19      *
     20      * @var $instance
     21      */
     22     public static $instance = null;
     23 
     24     /**
     25      * Adds various actions to set up the page
     26      *
     27      * @return self|void
     28      */
     29     public function __construct()
     30     {
     31         if (self::$instance) {
     32             return self::$instance;
     33         }
     34 
     35         self::$instance = $this;
     36         $this->loadScripts();
     37     }
     38 
     39     /**
     40      * Adds scripts to the admin
     41      *
     42      * @return void
     43      */
     44     public function loadScripts()
     45     {
     46         \add_action(
     47             'admin_enqueue_scripts',
     48             function ($hook) {
     49                 if (!current_user_can(App::$requiredCapability)) {
     50                     return;
     51                 }
     52 
     53                 if (!$this->checkItsGutenbergPost($hook)) {
     54                     return;
     55                 }
     56 
     57                 $this->addScopedScriptsAndStyles();
     58             }
     59         );
     60     }
     61 
     62     /**
     63      * Makes sure we are on the correct page
     64      *
     65      * @param string $hook - An optional hook provided by WP to identify the page.
     66      * @return boolean
     67      */
     68     public function checkItsGutenbergPost($hook = '')
     69     {
     70         if (isset($GLOBALS['typenow']) && \use_block_editor_for_post_type($GLOBALS['typenow'])) {
     71             return $hook && in_array($hook, ['post.php', 'post-new.php'], true);
     72         }
     73 
     74         return false;
     75     }
     76 
     77     /**
     78      * Adds various JS scripts
     79      *
     80      * @return void
     81      */
     82     public function addScopedScriptsAndStyles()
     83     {
     84         $version = App::$environment === 'PRODUCTION' ? App::$version : uniqid();
     85 
     86         \wp_register_script(
     87             App::$slug . '-scripts',
     88             EXTENDIFYSDK_BASE_URL . 'public/build/extendify-sdk.js',
     89             [
     90                 'wp-api',
     91                 'wp-i18n',
     92                 'wp-components',
     93                 'wp-element',
     94                 'wp-editor',
     95             ],
     96             $version,
     97             true
     98         );
     99         \wp_localize_script(
    100             App::$slug . '-scripts',
    101             'extendifySdkData',
    102             [
    103                 'root' => \esc_url_raw(rest_url(APP::$slug . '/' . APP::$apiVersion)),
    104                 'nonce' => \wp_create_nonce('wp_rest'),
    105                 'user' => json_decode(User::data('extendifysdk_user_data'), true),
    106                 'source' => \esc_attr(APP::$sourcePlugin),
    107             ]
    108         );
    109         \wp_enqueue_script(App::$slug . '-scripts');
    110 
    111         \wp_set_script_translations(App::$slug . '-scripts', App::$textDomain);
    112 
    113         \wp_enqueue_style(
    114             App::$slug . '-theme',
    115             EXTENDIFYSDK_BASE_URL . 'public/build/extendify-sdk.css',
    116             [],
    117             $version,
    118             'all'
    119         );
    120     }
    121 }