Completed
Push — develop ( cba97a...961eec )
by David
03:11
created

Wordlift_Admin::enqueue_styles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * The admin-specific functionality of the plugin.
5
 *
6
 * @link       https://wordlift.io
7
 * @since      1.0.0
8
 *
9
 * @package    Wordlift
10
 * @subpackage Wordlift/admin
11
 */
12
13
/**
14
 * The admin-specific functionality of the plugin.
15
 *
16
 * Defines the plugin name, version, and two examples hooks for how to
17
 * enqueue the admin-specific stylesheet and JavaScript.
18
 *
19
 * @package    Wordlift
20
 * @subpackage Wordlift/admin
21
 * @author     WordLift <[email protected]>
22
 */
23
class Wordlift_Admin {
24
25
	/**
26
	 * The ID of this plugin.
27
	 *
28
	 * @since    1.0.0
29
	 * @access   private
30
	 * @var      string $plugin_name The ID of this plugin.
31
	 */
32
	private $plugin_name;
33
34
	/**
35
	 * The version of this plugin.
36
	 *
37
	 * @since    1.0.0
38
	 * @access   private
39
	 * @var      string $version The current version of this plugin.
40
	 */
41
	private $version;
42
43
	/**
44
	 * The {@link Wordlift_Configuration_Service} instance.
45
	 *
46
	 * @since  3.14.0
47
	 * @access private
48
	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
49
	 */
50
	private $configuration_service;
51
52
	/**
53
	 * The {@link Wordlift_User_Service} instance.
54
	 *
55
	 * @since  3.14.0
56
	 * @access private
57
	 * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance.
58
	 */
59
	private $user_service;
60
61
	/**
62
	 * The {@link Wordlift_Batch_Operation_Ajax_Adapter} instance.
63
	 *
64
	 * @since 3.20.0
65
	 * @access private
66
	 * @var \Wordlift_Batch_Operation_Ajax_Adapter $sync_batch_operation_ajax_adapter The {@link Wordlift_Batch_Operation_Ajax_Adapter} instance.
67
	 */
68
	private $sync_batch_operation_ajax_adapter;
69
70
	/**
71
	 * The singleton instance.
72
	 *
73
	 * @since 3.19.4
74
	 * @access private
75
	 * @var Wordlift_Admin $instance The singleton instance.
76
	 */
77
	private static $instance;
78
79
	/**
80
	 * Initialize the class and set its properties.
81
	 *
82
	 * @since  1.0.0
83
	 *
84
	 * @param string                          $plugin_name The name of this plugin.
85
	 * @param string                          $version The version of this plugin.
86
	 * @param \Wordlift_Configuration_Service $configuration_service The configuration service.
87
	 * @param \Wordlift_Notice_Service        $notice_service The notice service.
88
	 * @param \Wordlift_User_Service          $user_service The {@link Wordlift_User_Service} instance.
89
	 */
90
	public function __construct( $plugin_name, $version, $configuration_service, $notice_service, $user_service ) {
91
92
		$this->plugin_name = $plugin_name;
93
		$this->version     = $version;
94
95
		$this->configuration_service = $configuration_service;
96
		$this->user_service          = $user_service;
97
98
		$dataset_uri = $configuration_service->get_dataset_uri();
99
		$key         = $configuration_service->get_key();
100
101
		if ( empty( $dataset_uri ) ) {
102
			$settings_page = Wordlift_Admin_Settings_Page::get_instance();
103
			if ( empty( $key ) ) {
104
				$error = sprintf( esc_html__( "WordLift's key isn't set, please open the %s to set WordLift's key.", 'wordlift' ), '<a href="' . $settings_page->get_url() . '">' . esc_html__( 'settings page', 'wordlift' ) . '</a>' );
0 ignored issues
show
Bug introduced by
The method get_url() does not seem to exist on object<Wordlift_Notice_Service>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
			} else {
106
				$error = sprintf( esc_html__( "WordLift's dataset URI is not configured: please open the %s to set WordLift's key again.", 'wordlift' ), '<a href="' . $settings_page->get_url() . '">' . esc_html__( 'settings page', 'wordlift' ) . '</a>' );
0 ignored issues
show
Bug introduced by
The method get_url() does not seem to exist on object<Wordlift_Notice_Service>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
			}
108
			$notice_service->add_error( $error );
109
		}
110
111
		// Load additional code if we're in the admin UI.
112
		if ( is_admin() ) {
113
114
			// Require the PHP files for the next code fragment.
115
			self::require_files();
116
117
			new Wordlift_Dashboard_Latest_News();
118
119
			// Search Rankings.
120
			$search_rankings_service = new Wordlift_Admin_Search_Rankings_Service( Wordlift_Api_Service::get_instance() );
121
			new Wordlift_Admin_Search_Rankings_Ajax_Adapter( $search_rankings_service );
122
123
			/*
124
			 * Add support for `All Entity Types`.
125
			 *
126
			 * @since 3.20.0
127
			 *
128
			 * @see https://github.com/insideout10/wordlift-plugin/issues/835
129
			 */
130
			if ( WL_ALL_ENTITY_TYPES ) {
131
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-schemaorg-taxonomy-metabox.php';
132
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-schemaorg-property-metabox.php';
133
134
				// new Wordlift_Admin_Schemaorg_Property_Metabox( Wordlift_Schemaorg_Property_Service::get_instance() );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
135
				/*
136
				 * The `Mappings` admin page.
137
				 */
138
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-mappings-page.php';
139
				new Wordlift_Admin_Mappings_Page();
140
141
				/*
142
				 * Allow sync'ing the schema.org taxonomy with the schema.org json file.
143
				 *
144
				 * @since 3.20.0
145
				 */
146
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/schemaorg/class-wordlift-schemaorg-sync-batch-operation.php';
147
148
				$this->sync_batch_operation_ajax_adapter = new Wordlift_Batch_Operation_Ajax_Adapter( new Wordlift_Schemaorg_Sync_Batch_Operation(), 'wl_schemaorg_sync' );
149
150
			}
151
152
			/*
153
			 * Add the {@link Wordlift_Admin_Term_Adapter}.
154
			 *
155
			 * @since 3.20.0
156
			 */
157
			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-term-adapter.php';
158
			new Wordlift_Admin_Term_Adapter();
159
160
			/*
161
			 * The new dashboard.
162
			 *
163
			 * @since 3.20.0
164
			 *
165
			 * @see https://github.com/insideout10/wordlift-plugin/issues/879
166
			 */
167
			new Wordlift_Admin_Dashboard_V2(
168
				$search_rankings_service,
169
				Wordlift::get_instance()->get_dashboard_service(),
170
				Wordlift_Entity_Service::get_instance()
171
			);
172
			new Wordlift_Admin_Not_Enriched_Filter();
173
174
		}
175
176
		// Set the singleton instance.
177
		self::$instance = $this;
178
179
	}
180
181
	/**
182
	 * Get the singleton instance.
183
	 *
184
	 * @since 3.19.4
185
	 *
186
	 * @return \Wordlift_Admin The singleton instance.
187
	 */
188
	public static function get_instance() {
189
190
		return self::$instance;
191
	}
192
193
	/**
194
	 * Register the stylesheets for the admin area.
195
	 *
196
	 * @since    1.0.0
197
	 */
198
	public function enqueue_styles() {
199
200
		/**
201
		 * This function is provided for demonstration purposes only.
202
		 *
203
		 * An instance of this class should be passed to the run() function
204
		 * defined in Wordlift_Loader as all of the hooks are defined
205
		 * in that particular class.
206
		 *
207
		 * The Wordlift_Loader will then create the relationship
208
		 * between the defined hooks and the functions defined in this
209
		 * class.
210
		 */
211
212
		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' );
213
214
	}
215
216
	/**
217
	 * Register the JavaScript for the admin area.
218
	 *
219
	 * @since    1.0.0
220
	 */
221
	public function enqueue_scripts() {
222
223
		/**
224
		 * This function is provided for demonstration purposes only.
225
		 *
226
		 * An instance of this class should be passed to the run() function
227
		 * defined in Wordlift_Loader as all of the hooks are defined
228
		 * in that particular class.
229
		 *
230
		 * The Wordlift_Loader will then create the relationship
231
		 * between the defined Wordlift_Schemaorg_Property_Servicehooks and the functions defined in this
232
		 * class.
233
		 */
234
235
		// Enqueue the admin scripts.
236
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/1/admin.js', array(
237
			'jquery',
238
			'underscore',
239
			'backbone',
240
		), $this->version, false );
241
242
243
		$can_edit_wordlift_entities = current_user_can( 'edit_wordlift_entities' );
244
245
		/*
246
		 * People that can create entities will see the scope set in the wp-config.php file (by default `cloud`). People
247
		 * that cannot edit create entities will always see the local entities.
248
		 *
249
		 * @see https://github.com/insideout10/wordlift-plugin/issues/839
250
		 */
251
		$autocomplete_scope = $can_edit_wordlift_entities ? WL_AUTOCOMPLETE_SCOPE : "local";
252
253
		// Set the basic params.
254
		$params = array(
255
			// @todo scripts in admin should use wp.post.
256
			'ajax_url'                   => admin_url( 'admin-ajax.php' ),
257
			// @todo remove specific actions from settings.
258
			'action'                     => 'entity_by_title',
259
			'datasetUri'                 => $this->configuration_service->get_dataset_uri(),
260
			'language'                   => $this->configuration_service->get_language_code(),
261
			'link_by_default'            => $this->configuration_service->is_link_by_default(),
262
			// Whether the current user is allowed to create new entities.
263
			//
264
			// @see https://github.com/insideout10/wordlift-plugin/issues/561
265
			'can_create_entities'        => $can_edit_wordlift_entities ? 'yes' : 'no',
266
			'l10n'                       => array(
267
				'You already published an entity with the same name'                 => __( 'You already published an entity with the same name: ', 'wordlift' ),
268
				'logo_selection_title'                                               => __( 'WordLift Choose Logo', 'wordlift' ),
269
				'logo_selection_button'                                              => array( 'text' => __( 'Choose Logo', 'wordlift' ) ),
270
				'Type at least 3 characters to search...'                            => _x( 'Type at least 3 characters to search...', 'Autocomplete Select', 'wordlift' ),
271
				'No results found for your search.'                                  => _x( 'No results found: try changing or removing some words.', 'Autocomplete Select', 'wordlift' ),
272
				'Please wait while we look for entities in the linked data cloud...' => _x( 'Please wait while we look for entities in the linked data cloud...', 'Autocomplete Select', 'wordlift' ),
273
				'Add keywords to track'                                              => __( 'Add Keywords to track', 'wordlift' ),
274
			),
275
			'wl_autocomplete_nonce'      => wp_create_nonce( 'wordlift_autocomplete' ),
276
			'autocomplete_scope'         => $autocomplete_scope,
277
			/**
278
			 * Allow 3rd parties to define the default editor id. This turns useful if 3rd parties load
279
			 * or change the TinyMCE id.
280
			 *
281
			 * The editor id is currently referenced by `src/coffee/editpost-widget/app.services.EditorAdapter.coffee`.
282
			 *
283
			 * @since 3.19.4
284
			 *
285
			 * @see https://github.com/insideout10/wordlift-plugin/issues/848
286
			 *
287
			 * @param string $editor The default editor id, by default `content`.
288
			 */
289
			'default_editor_id'          => apply_filters( 'wl_default_editor_id', 'content' ),
290
			/**
291
			 * Add the link to the Search Keywords admin page.
292
			 *
293
			 * @since 3.20.0
294
			 */
295
			'search_keywords_admin_page' => admin_url( 'admin.php?page=wl_configuration_admin_menu&tab=search-keywords' ),
296
		);
297
298
		// Set post-related values if there's a current post.
299
		if ( null !== $post = $entity_being_edited = get_post() ) {
300
301
			$params['post_id']           = $entity_being_edited->ID;
302
			$entity_service              = Wordlift_Entity_Service::get_instance();
303
			$params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() );
304
			// We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it
305
			// from the results, since we don't want the current entity to be discovered by the analysis.
306
			//
307
			// See https://github.com/insideout10/wordlift-plugin/issues/345
308
			$params['itemId']                      = $entity_service->get_uri( $entity_being_edited->ID );
309
			$params['wl_schemaorg_property_nonce'] = wp_create_nonce( 'wl_schemaorg_property' );
310
311
			/*
312
			 * Add the `properties` if `WL_ALL_ENTITY_TYPES` is enabled.
313
			 *
314
			 * @see https://github.com/insideout10/wordlift-plugin/issues/835
315
			 */
316
			if ( WL_ALL_ENTITY_TYPES ) {
317
				$params['properties'] = Wordlift_Schemaorg_Property_Service::get_instance()->get_all( $post->ID );
318
			}
319
320
		}
321
322
		// Finally output the params as `wlSettings` for JavaScript code.
323
		wp_localize_script( $this->plugin_name, 'wlSettings', apply_filters( 'wl_admin_settings', $params ) );
324
325
	}
326
327
	/**
328
	 * Require files needed for the Admin UI.
329
	 *
330
	 * @since 3.20.0
331
	 */
332
	private static function require_files() {
333
334
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-dashboard-latest-news.php';
335
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-search-rankings-service.php';
336
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-search-rankings-ajax-adapter.php';
337
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-dashboard-v2.php';
338
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-not-enriched-filter.php';
339
340
	}
341
342
	public static function is_gutenberg() {
343
		if ( function_exists( 'is_gutenberg_page' ) &&
344
		     is_gutenberg_page()
345
		) {
346
			// The Gutenberg plugin is on.
347
			return true;
348
		}
349
		$current_screen = get_current_screen();
350
		if ( method_exists( $current_screen, 'is_block_editor' ) &&
351
		     $current_screen->is_block_editor()
352
		) {
353
			// Gutenberg page on 5+.
354
			return true;
355
		}
356
		return false;
357
	}
358
359
}
360