| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Hooks\Actions; |
| 4 |
|
| 5 |
use FL\Assistant\Data\Repository\PostsRepository; |
| 6 |
use FL\Assistant\Data\Repository\UsersRepository; |
| 7 |
use FL\Assistant\Data\Site; |
| 8 |
use FL\Assistant\Data\Transformers\UserTransformer; |
| 9 |
use FL\Assistant\Data\UserState; |
| 10 |
use FLBuilderModel; |
| 11 |
use WP_REST_Request; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class OnEnqueueScripts |
| 15 |
* @package FL\Assistant\Hooks\Actions |
| 16 |
* |
| 17 |
*/ |
| 18 |
class OnEnqueueScripts { |
| 19 |
|
| 20 |
/** |
| 21 |
* @var UsersRepository |
| 22 |
*/ |
| 23 |
protected $users; |
| 24 |
/** |
| 25 |
* @var PostsRepository |
| 26 |
*/ |
| 27 |
protected $posts; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var Site |
| 31 |
*/ |
| 32 |
protected $site; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var UserTransformer |
| 36 |
*/ |
| 37 |
protected $user_transform; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* OnEnqueueScripts constructor. |
| 42 |
* |
| 43 |
* @param UsersRepository $users |
| 44 |
* @param PostsRepository $posts |
| 45 |
* @param Site $site |
| 46 |
* @param UserTransformer $user_transform |
| 47 |
*/ |
| 48 |
public function __construct( |
| 49 |
UsersRepository $users, |
| 50 |
PostsRepository $posts, |
| 51 |
Site $site, |
| 52 |
UserTransformer $user_transform |
| 53 |
) { |
| 54 |
|
| 55 |
$this->users = $users; |
| 56 |
$this->posts = $posts; |
| 57 |
$this->site = $site; |
| 58 |
$this->user_transform = $user_transform; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* @return array |
| 64 |
* @throws \Exception |
| 65 |
*/ |
| 66 |
public function generate_initial_state() { |
| 67 |
if ( ! is_user_logged_in() ) { |
| 68 |
return []; |
| 69 |
} |
| 70 |
|
| 71 |
$user_state = UserState::get(); |
| 72 |
|
| 73 |
// Always show in admin bar and hidden in wp-admin. |
| 74 |
if ( is_admin() ) { |
| 75 |
$user_state['window']['isHidden'] = true; |
| 76 |
$user_state['window']['hiddenAppearance'] = 'admin_bar'; |
| 77 |
} |
| 78 |
|
| 79 |
return [ |
| 80 |
'appOrder' => $user_state['appOrder'], |
| 81 |
'counts' => $this->get_counts(), |
| 82 |
'shouldReduceMotion' => false, /* Disabled */ |
| 83 |
|
| 84 |
/* New UI Props */ |
| 85 |
'appearance' => $user_state['appearance'], |
| 86 |
'history' => $user_state['history'], |
| 87 |
'searchHistory' => $user_state['searchHistory'], |
| 88 |
'shouldShowLabels' => false, /* Disabled */ |
| 89 |
'window' => $user_state['window'], |
| 90 |
]; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* |
| 96 |
* Compiles an array of all assistant data. |
| 97 |
* |
| 98 |
* NOTE: Kept in alphabetical order. |
| 99 |
* |
| 100 |
* @return array |
| 101 |
* @throws \Exception |
| 102 |
*/ |
| 103 |
public function generate_frontend_config() { |
| 104 |
|
| 105 |
$current_user = $this->users->current( $this->user_transform ); |
| 106 |
|
| 107 |
return [ |
| 108 |
'adminURLs' => $this->site->get_admin_urls(), |
| 109 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 110 |
'apiRoot' => esc_url_raw( get_rest_url() ), |
| 111 |
'cloudUrl' => FL_ASSISTANT_CLOUD_URL, |
| 112 |
'contentTypes' => $this->posts->get_types(), |
| 113 |
'contentStatus' => $this->posts->get_stati(), |
| 114 |
'currentPageView' => $this->site->get_current_view(), |
| 115 |
'currentUser' => $current_user, |
| 116 |
'defaultAppName' => 'fl-dashboard', |
| 117 |
'emptyTrashDays' => EMPTY_TRASH_DAYS, |
| 118 |
'isShowingAdminBar' => is_admin_bar_showing(), |
| 119 |
'isAdmin' => is_admin(), |
| 120 |
'nonce' => [ |
| 121 |
'api' => wp_create_nonce( 'wp_rest' ), |
| 122 |
'reply' => wp_create_nonce( 'replyto-comment' ), |
| 123 |
'replyUnfiltered' => wp_create_nonce( 'unfiltered-html-comment' ), |
| 124 |
'updates' => wp_create_nonce( 'updates' ), |
| 125 |
], |
| 126 |
'pluginURL' => FL_ASSISTANT_URL, |
| 127 |
'taxonomies' => $this->posts->get_taxononies(), |
| 128 |
'userRoles' => $this->users->get_roles(), |
| 129 |
]; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns an array of all counts to hydrate the store. |
| 134 |
*/ |
| 135 |
public function get_counts() { |
| 136 |
$request = new WP_REST_Request( 'GET', '/fl-assistant/v1/counts' ); |
| 137 |
$response = rest_do_request( $request ); |
| 138 |
|
| 139 |
return $response->get_data(); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Check if the frontend scripts/styles should be enqueued |
| 144 |
*/ |
| 145 |
public function should_enqueue() { |
| 146 |
|
| 147 |
// Users must be logged in. |
| 148 |
if ( ! is_user_logged_in() ) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
// Don't show Assistant in customizer. |
| 153 |
if ( is_customize_preview() ) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
// Don't show in admin iframes. |
| 158 |
if ( defined( 'IFRAME_REQUEST' ) && IFRAME_REQUEST ) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
// User is logged in, and the current request is not customizer or an iframe |
| 163 |
$state = UserState::get(); |
| 164 |
|
| 165 |
// Don't show Assistant in the WP Admin if the user has turned it off. |
| 166 |
if ( is_admin() && ! $state['shouldShowInAdmin'] ) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
// There is no read-only assistant (for now). Users must be able to edit. |
| 171 |
if ( ! current_user_can( 'edit_others_posts' ) ) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Enqueue all scripts and styles |
| 180 |
*/ |
| 181 |
public function enqueue() { |
| 182 |
|
| 183 |
$url = FL_ASSISTANT_URL; |
| 184 |
$ver = FL_ASSISTANT_VERSION; |
| 185 |
|
| 186 |
wp_register_script( 'fl-fluid', $url . 'build/fl-assistant-fluid.bundle.js', [ 'react', 'react-dom' ], $ver, false ); |
| 187 |
wp_register_style( 'fl-fluid', $url . 'build/fl-assistant-fluid.bundle.css', [], $ver, null ); |
| 188 |
wp_enqueue_media(); |
| 189 |
if ( $this->should_enqueue() ) { |
| 190 |
|
| 191 |
$config = $this->generate_frontend_config(); |
| 192 |
$state = $this->generate_initial_state(); |
| 193 |
|
| 194 |
// API - loaded in header |
| 195 |
$js_deps = [ |
| 196 |
'fl-fluid', |
| 197 |
'lodash', |
| 198 |
'heartbeat', |
| 199 |
'wp-i18n', |
| 200 |
'wp-keycodes', |
| 201 |
'wp-dom-ready', |
| 202 |
'wp-components' |
| 203 |
]; |
| 204 |
|
| 205 |
wp_enqueue_style( 'fl-assistant-system', $url . 'build/fl-assistant-system.bundle.css', [ 'fl-fluid', 'wp-components' ], $ver, null ); |
| 206 |
wp_enqueue_script( 'fl-assistant-system', $url . 'build/fl-assistant-system.bundle.js', $js_deps, $ver, false ); |
| 207 |
|
| 208 |
wp_localize_script( 'fl-assistant-system', 'FL_ASSISTANT_CONFIG', $config ); |
| 209 |
wp_localize_script( 'fl-assistant-system', 'FL_ASSISTANT_INITIAL_STATE', $state ); |
| 210 |
|
| 211 |
// Apps - loaded in header |
| 212 |
wp_enqueue_style( 'fl-assistant-apps', $url . 'build/fl-assistant-apps.bundle.css', [], $ver, null ); |
| 213 |
wp_enqueue_script( 'fl-assistant-apps', $url . 'build/fl-assistant-apps.bundle.js', $js_deps, $ver, false ); |
| 214 |
|
| 215 |
// UI Render - loaded in footer |
| 216 |
wp_enqueue_style( 'fl-assistant-render', $url . 'build/fl-assistant-render.bundle.css', [], $ver, null ); |
| 217 |
wp_enqueue_script( 'fl-assistant-render', $url . 'build/fl-assistant-render.bundle.js', $js_deps, $ver, true ); |
| 218 |
|
| 219 |
do_action( 'fl_assistant_enqueue' ); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
public function __invoke() { |
| 224 |
$this->enqueue(); |
| 225 |
} |
| 226 |
} |
| 227 |
|