| @@ -2,9 +2,13 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Code_Snippets; |
| 4 | 4 | |
| 5 | 5 | use Code_Snippets\Admin\Bootstrap_Admin; |
| 6 | -use Code_Snippets\Client\Cloud_API; | |
| 6 | +use Code_Snippets\Admin\Feedback_Error_Capture; | |
| 7 | +use Code_Snippets\Admin\Feedback_Panel; | |
| 8 | +use Code_Snippets\Admin\Menus\Manage\Manage_Menu; | |
| 9 | +use Code_Snippets\Client\Feedback_Client; | |
| 10 | +use Code_Snippets\Controller\Cloud_Search_Controller; | |
| 7 | 11 | use Code_Snippets\Core\DB; |
| 8 | 12 | use Code_Snippets\Core\Licensing; |
| 9 | 13 | use Code_Snippets\Core\Upgrader; |
| 10 | 14 | use Code_Snippets\Integration\Admin_Bar; |
| @@ -12,13 +16,20 @@ | ||
| 12 | 16 | use Code_Snippets\Integration\Evaluate_Content; |
| 13 | 17 | use Code_Snippets\Integration\Evaluate_Functions; |
| 14 | 18 | use Code_Snippets\Integration\Promotions\Promotion_Manager; |
| 15 | 19 | use Code_Snippets\Integration\Shortcodes; |
| 20 | +use Code_Snippets\Model\Basic_Cloud_Connection; | |
| 21 | +use Code_Snippets\Model\Feedback_Connection; | |
| 16 | 22 | use Code_Snippets\REST_API\Cloud\Cloud_Snippets_REST_Controller; |
| 23 | +use Code_Snippets\REST_API\Feedback\Feedback_REST_Controller; | |
| 17 | 24 | use Code_Snippets\REST_API\Import\File_Import_REST_Controller; |
| 18 | 25 | use Code_Snippets\REST_API\Import\Plugins_Import_REST_Controller; |
| 26 | +use Code_Snippets\REST_API\Preferences\Demos_Seen_REST_Controller; | |
| 27 | +use Code_Snippets\REST_API\Preferences\Insights_View_Rest_Controller; | |
| 28 | +use Code_Snippets\REST_API\Preferences\Snippet_View_REST_Controller; | |
| 19 | 29 | use Code_Snippets\REST_API\Snippets\Recently_Active_REST_Controller; |
| 20 | 30 | use Code_Snippets\REST_API\Snippets\Snippets_REST_Controller; |
| 31 | +use function Code_Snippets\Settings\are_settings_unified; | |
| 21 | 32 | |
| 22 | 33 | /** |
| 23 | 34 | * The main plugin class |
| 24 | 35 | * |
| @@ -51,22 +62,15 @@ | ||
| 51 | 62 | */ |
| 52 | 63 | public DB $db; |
| 53 | 64 | |
| 54 | 65 | /** |
| 55 | - * Class for evaluating function snippets. | |
| 66 | + * Class for managing shortcodes. | |
| 56 | 67 | * |
| 57 | - * @var Evaluate_Functions | |
| 68 | + * @var Shortcodes | |
| 58 | 69 | */ |
| 59 | - public Evaluate_Functions $evaluate_functions; | |
| 70 | + public Shortcodes $shortcodes; | |
| 60 | 71 | |
| 61 | 72 | /** |
| 62 | - * Class for evaluating content snippets. | |
| 63 | - * | |
| 64 | - * @var Evaluate_Content | |
| 65 | - */ | |
| 66 | - public Evaluate_Content $evaluate_content; | |
| 67 | - | |
| 68 | - /** | |
| 69 | 73 | * Administration area class |
| 70 | 74 | * |
| 71 | 75 | * @var Bootstrap_Admin |
| 72 | 76 | */ |
| @@ -72,15 +76,8 @@ | ||
| 72 | 76 | */ |
| 73 | 77 | public Bootstrap_Admin $admin; |
| 74 | 78 | |
| 75 | 79 | /** |
| 76 | - * Class for managing cloud API actions. | |
| 77 | - * | |
| 78 | - * @var Cloud_API | |
| 79 | - */ | |
| 80 | - public Cloud_API $cloud_api; | |
| 81 | - | |
| 82 | - /** | |
| 83 | 80 | * Handles licensing and plugin updates. |
| 84 | 81 | * |
| 85 | 82 | * @var Licensing |
| 86 | 83 | */ |
| @@ -93,8 +90,15 @@ | ||
| 93 | 90 | */ |
| 94 | 91 | public Flat_Files\Handler_Registry $snippet_handler_registry; |
| 95 | 92 | |
| 96 | 93 | /** |
| 94 | + * Instance of connection to Code Snippets Cloud. | |
| 95 | + * | |
| 96 | + * @var Basic_Cloud_Connection | |
| 97 | + */ | |
| 98 | + public Basic_Cloud_Connection $cloud_connection; | |
| 99 | + | |
| 100 | + /** | |
| 97 | 101 | * Class constructor |
| 98 | 102 | */ |
| 99 | 103 | public function __construct() { |
| 100 | 104 | wp_cache_add_global_groups( CACHE_GROUP ); |
| @@ -104,28 +108,34 @@ | ||
| 104 | 108 | /** |
| 105 | 109 | * Load the plugin utilities. |
| 106 | 110 | */ |
| 107 | 111 | private function load_utilities() { |
| 108 | - require_once __DIR__ . '/snippet-ops.php'; | |
| 109 | 112 | require_once __DIR__ . '/Utils/editor.php'; |
| 110 | 113 | require_once __DIR__ . '/Utils/options.php'; |
| 111 | 114 | require_once __DIR__ . '/Settings/settings.php'; |
| 115 | + require_once __DIR__ . '/snippet-ops.php'; | |
| 112 | 116 | } |
| 113 | 117 | |
| 114 | 118 | /** |
| 115 | - * Initialise the plugin and load all necessary classes. | |
| 119 | + * Initialize the plugin and load all necessary classes. | |
| 116 | 120 | */ |
| 117 | 121 | public function load_plugin() { |
| 118 | 122 | $this->load_utilities(); |
| 119 | 123 | |
| 120 | 124 | $this->db = new DB(); |
| 121 | - $this->cloud_api = new Cloud_API(); | |
| 122 | 125 | $this->licensing = new Licensing(); |
| 123 | - $this->evaluate_content = new Evaluate_Content( $this->db ); | |
| 124 | - $this->evaluate_functions = new Evaluate_Functions( $this->db ); | |
| 125 | 126 | |
| 127 | + $this->cloud_connection = new Basic_Cloud_Connection(); | |
| 128 | + | |
| 129 | + new Evaluate_Content( $this->db ); | |
| 130 | + new Evaluate_Functions( $this->db ); | |
| 131 | + | |
| 132 | + $this->shortcodes = new Shortcodes(); | |
| 133 | + | |
| 126 | 134 | if ( is_admin() ) { |
| 127 | 135 | $this->admin = new Bootstrap_Admin(); |
| 136 | + new Promotion_Manager(); | |
| 137 | + new Feedback_Error_Capture( new Feedback_Panel() ); | |
| 128 | 138 | } |
| 129 | 139 | |
| 130 | 140 | new Shortcodes(); |
| 131 | 141 | new MCE_Plugin(); |
| @@ -131,12 +141,8 @@ | ||
| 131 | 141 | new MCE_Plugin(); |
| 132 | 142 | new Upgrader( PLUGIN_VERSION, $this->db ); |
| 133 | 143 | new Admin_Bar(); |
| 134 | 144 | |
| 135 | - if ( is_admin() ) { | |
| 136 | - new Promotion_Manager(); | |
| 137 | - } | |
| 138 | - | |
| 139 | 145 | $this->init_snippet_files(); |
| 140 | 146 | |
| 141 | 147 | add_action( 'rest_api_init', [ $this, 'init_rest_api' ], 1 ); |
| 142 | 148 | } |
| @@ -146,17 +152,27 @@ | ||
| 146 | 152 | * |
| 147 | 153 | * @return void |
| 148 | 154 | */ |
| 149 | 155 | public function init_rest_api() { |
| 156 | + $cloud_search = new Cloud_Search_Controller( $this->cloud_connection ); | |
| 157 | + | |
| 150 | 158 | new Snippets_REST_Controller(); |
| 151 | 159 | new Recently_Active_REST_Controller(); |
| 160 | + | |
| 161 | + new Snippet_View_REST_Controller(); | |
| 162 | + new Insights_View_Rest_Controller(); | |
| 163 | + new Demos_Seen_REST_Controller(); | |
| 164 | + | |
| 152 | 165 | new Plugins_Import_REST_Controller(); |
| 153 | 166 | new File_Import_REST_Controller(); |
| 154 | - new Cloud_Snippets_REST_Controller( $this->cloud_api ); | |
| 167 | + | |
| 168 | + new Cloud_Snippets_REST_Controller( $cloud_search ); | |
| 169 | + | |
| 170 | + new Feedback_REST_Controller( new Feedback_Client( new Feedback_Connection() ) ); | |
| 155 | 171 | } |
| 156 | 172 | |
| 157 | 173 | /** |
| 158 | - * Initialises the snippet files component. | |
| 174 | + * Initializes the snippet files component. | |
| 159 | 175 | * |
| 160 | 176 | * @return void |
| 161 | 177 | */ |
| 162 | 178 | private function init_snippet_files() { |
| @@ -195,8 +211,9 @@ | ||
| 195 | 211 | $edit = [ 'edit', 'edit-snippet' ]; |
| 196 | 212 | $import = [ 'import', 'import-snippets', 'import-code-snippets' ]; |
| 197 | 213 | $settings = [ 'settings', 'snippets-settings' ]; |
| 198 | 214 | $cloud = [ 'cloud', 'cloud-snippets' ]; |
| 215 | + $insights = [ 'insights', 'code-snippets-insights' ]; | |
| 199 | 216 | $welcome = [ 'welcome', 'getting-started', 'code-snippets' ]; |
| 200 | 217 | |
| 201 | 218 | if ( in_array( $menu, $edit, true ) ) { |
| 202 | 219 | return 'edit-snippet'; |
| @@ -207,8 +224,10 @@ | ||
| 207 | 224 | } elseif ( in_array( $menu, $settings, true ) ) { |
| 208 | 225 | return 'snippets-settings'; |
| 209 | 226 | } elseif ( in_array( $menu, $cloud, true ) ) { |
| 210 | 227 | return 'snippets&subpage=cloud-community'; |
| 228 | + } elseif ( in_array( $menu, $insights, true ) ) { | |
| 229 | + return 'code-snippets-insights'; | |
| 211 | 230 | } elseif ( in_array( $menu, $welcome, true ) ) { |
| 212 | 231 | return 'code-snippets-welcome'; |
| 213 | 232 | } else { |
| 214 | 233 | return 'snippets'; |
| @@ -236,21 +255,28 @@ | ||
| 236 | 255 | } else { |
| 237 | 256 | $url = 'admin.php?page=' . $slug; |
| 238 | 257 | } |
| 239 | 258 | |
| 240 | - if ( 'network' === $context ) { | |
| 259 | + if ( 'snippets-settings' === $slug && are_settings_unified() ) { | |
| 241 | 260 | return network_admin_url( $url ); |
| 242 | - } elseif ( 'admin' === $context ) { | |
| 243 | - return admin_url( $url ); | |
| 244 | - } else { | |
| 245 | - return self_admin_url( $url ); | |
| 246 | 261 | } |
| 262 | + | |
| 263 | + switch ( $context ) { | |
| 264 | + case 'network': | |
| 265 | + return network_admin_url( $url ); | |
| 266 | + | |
| 267 | + case 'admin': | |
| 268 | + return admin_url( $url ); | |
| 269 | + | |
| 270 | + default: | |
| 271 | + return self_admin_url( $url ); | |
| 272 | + } | |
| 247 | 273 | } |
| 248 | 274 | |
| 249 | 275 | /** |
| 250 | 276 | * Allow redirecting to the Code Snippets site. |
| 251 | 277 | * |
| 252 | - * @param array<string> $hosts Allowed hosts. | |
| 278 | + * @param string[] $hosts Allowed hosts. | |
| 253 | 279 | * |
| 254 | 280 | * @return array Modified allowed hosts. |
| 255 | 281 | */ |
| 256 | 282 | public function allow_code_snippets_redirect( array $hosts ): array { |
| @@ -328,9 +354,9 @@ | ||
| 328 | 354 | : $this->get_cap_name(); |
| 329 | 355 | } |
| 330 | 356 | |
| 331 | 357 | /** |
| 332 | - * Localise a plugin script to provide the CODE_SNIPPETS object. | |
| 358 | + * Localize a plugin script to provide the CODE_SNIPPETS object. | |
| 333 | 359 | * |
| 334 | 360 | * @param string $handle Script handle. |
| 335 | 361 | * |
| 336 | 362 | * @return void |
| @@ -339,32 +365,43 @@ | ||
| 339 | 365 | wp_localize_script( |
| 340 | 366 | $handle, |
| 341 | 367 | 'CODE_SNIPPETS', |
| 342 | 368 | [ |
| 343 | - 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, | |
| 344 | - 'isLicensed' => $this->licensing->is_licensed(), | |
| 345 | - 'isCloudConnected' => $this->cloud_api->is_cloud_connection_available(), | |
| 346 | - 'hideUpsell' => Settings\get_setting( 'general', 'hide_upgrade_menu' ), | |
| 347 | - 'restAPI' => [ | |
| 369 | + 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, | |
| 370 | + 'isLicensed' => $this->licensing->is_licensed(), | |
| 371 | + 'isCloudConnected' => $this->cloud_connection->is_authenticated(), | |
| 372 | + 'hideUpsell' => Settings\get_setting( 'general', 'hide_upgrade_menu' ), | |
| 373 | + 'snippetView' => Snippet_View_REST_Controller::get_snippet_view(), | |
| 374 | + 'insightsChartViews' => Insights_View_Rest_Controller::get_insights_chart_views(), | |
| 375 | + 'demosSeen' => Demos_Seen_REST_Controller::get_demos_seen(), | |
| 376 | + 'restAPI' => [ | |
| 348 | 377 | 'base' => esc_url_raw( rest_url() ), |
| 349 | 378 | 'snippets' => esc_url_raw( rest_url( Snippets_REST_Controller::get_base_route() ) ), |
| 350 | 379 | 'recentlyActive' => esc_url_raw( rest_url( Recently_Active_REST_Controller::get_base_route() ) ), |
| 380 | + 'snippetView' => esc_url_raw( rest_url( Snippet_View_REST_Controller::get_base_route() ) ), | |
| 381 | + 'insightsView' => esc_url_raw( rest_url( Insights_View_Rest_Controller::get_base_route() ) ), | |
| 382 | + 'demosSeen' => esc_url_raw( rest_url( Demos_Seen_REST_Controller::get_base_route() ) ), | |
| 351 | 383 | 'importPlugins' => esc_url_raw( rest_url( Plugins_Import_REST_Controller::get_base_route() ) ), |
| 352 | 384 | 'importFiles' => esc_url_raw( rest_url( File_Import_REST_Controller::get_base_route() ) ), |
| 353 | 385 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 354 | 386 | 'cloud' => [ |
| 355 | - 'token' => $this->cloud_api->get_local_token(), | |
| 387 | + 'token' => $this->cloud_connection->get_local_token(), | |
| 356 | 388 | 'snippets' => esc_url_raw( rest_url( Cloud_Snippets_REST_Controller::get_base_route() ) ), |
| 357 | 389 | ], |
| 358 | 390 | ], |
| 359 | - 'urls' => [ | |
| 360 | - 'plugin' => esc_url_raw( plugins_url( '', PLUGIN_FILE ) ), | |
| 361 | - 'manage' => esc_url_raw( $this->get_menu_url() ), | |
| 362 | - 'edit' => esc_url_raw( $this->get_menu_url( 'edit' ) ), | |
| 363 | - 'addNew' => esc_url_raw( $this->get_menu_url( 'add' ) ), | |
| 364 | - 'welcome' => esc_url_raw( $this->get_menu_url( 'welcome' ) ), | |
| 365 | - 'settings' => esc_url_raw( $this->get_menu_url( 'settings' ) ), | |
| 366 | - 'cloud' => esc_url_raw( $this->cloud_api->get_cloud_url() ), | |
| 391 | + 'urls' => [ | |
| 392 | + 'plugin' => esc_url_raw( plugins_url( '', PLUGIN_FILE ) ), | |
| 393 | + 'manage' => esc_url_raw( $this->get_menu_url() ), | |
| 394 | + 'edit' => esc_url_raw( $this->get_menu_url( 'edit' ) ), | |
| 395 | + 'addNew' => esc_url_raw( $this->get_menu_url( 'add' ) ), | |
| 396 | + 'insights' => esc_url_raw( $this->get_menu_url( 'insights' ) ), | |
| 397 | + 'welcome' => esc_url_raw( $this->get_menu_url( 'welcome' ) ), | |
| 398 | + 'import' => esc_url_raw( $this->get_menu_url( 'import' ) ), | |
| 399 | + 'cloud' => esc_url_raw( $this->cloud_connection->get_base_url() ), | |
| 400 | + 'demoReset' => esc_url_raw( Manage_Menu::get_demo_reset_url() ), | |
| 401 | + 'settings' => ! are_settings_unified() || is_super_admin() | |
| 402 | + ? esc_url_raw( $this->get_menu_url( 'settings' ) ) | |
| 403 | + : null, | |
| 367 | 404 | ], |
| 368 | 405 | ] |
| 369 | 406 | ); |
| 370 | 407 | } |