Completed
Push — develop ( fd2432...e25aed )
by David
03:47
created

Wordlift_Admin   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 337
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
dl 0
loc 337
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 90 5
A get_instance() 0 4 1
A enqueue_styles() 0 17 1
C enqueue_scripts() 0 104 9
A require_files() 0 9 1
A is_gutenberg() 0 17 5
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
		 * Do not load our scripts on the Filter Urls plugin admin pages.
225
		 *
226
		 * @see https://github.com/insideout10/wordlift-plugin/issues/901
227
		 * @since 3.20.0
228
		 */
229
		$screen = get_current_screen();
230
		if ( is_a( $screen, 'WP_Screen' ) && 'filter-urls_page_filter_urls_form' === $screen->id ) {
231
			return;
232
		}
233
234
		// Enqueue the admin scripts.
235
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/1/admin.js', array(
236
			'jquery',
237
			'underscore',
238
			'backbone',
239
		), $this->version, false );
240
241
242
		$can_edit_wordlift_entities = current_user_can( 'edit_wordlift_entities' );
243
244
		/*
245
		 * People that can create entities will see the scope set in the wp-config.php file (by default `cloud`). People
246
		 * that cannot edit create entities will always see the local entities.
247
		 *
248
		 * @see https://github.com/insideout10/wordlift-plugin/issues/839
249
		 */
250
		$autocomplete_scope = $can_edit_wordlift_entities ? WL_AUTOCOMPLETE_SCOPE : "local";
251
252
		// Set the basic params.
253
		$params = array(
254
			// @todo scripts in admin should use wp.post.
255
			'ajax_url'                   => admin_url( 'admin-ajax.php' ),
256
			// @todo remove specific actions from settings.
257
			'action'                     => 'entity_by_title',
258
			'datasetUri'                 => $this->configuration_service->get_dataset_uri(),
259
			'language'                   => $this->configuration_service->get_language_code(),
260
			'link_by_default'            => $this->configuration_service->is_link_by_default(),
261
			// Whether the current user is allowed to create new entities.
262
			//
263
			// @see https://github.com/insideout10/wordlift-plugin/issues/561
264
			'can_create_entities'        => $can_edit_wordlift_entities ? 'yes' : 'no',
265
			'l10n'                       => array(
266
				'You already published an entity with the same name'                 => __( 'You already published an entity with the same name: ', 'wordlift' ),
267
				'logo_selection_title'                                               => __( 'WordLift Choose Logo', 'wordlift' ),
268
				'logo_selection_button'                                              => array( 'text' => __( 'Choose Logo', 'wordlift' ) ),
269
				'Type at least 3 characters to search...'                            => _x( 'Type at least 3 characters to search...', 'Autocomplete Select', 'wordlift' ),
270
				'No results found for your search.'                                  => _x( 'No results found: try changing or removing some words.', 'Autocomplete Select', 'wordlift' ),
271
				'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' ),
272
				'Add keywords to track'                                              => __( 'Add Keywords to track', 'wordlift' ),
273
			),
274
			'wl_autocomplete_nonce'      => wp_create_nonce( 'wordlift_autocomplete' ),
275
			'autocomplete_scope'         => $autocomplete_scope,
276
			/**
277
			 * Allow 3rd parties to define the default editor id. This turns useful if 3rd parties load
278
			 * or change the TinyMCE id.
279
			 *
280
			 * The editor id is currently referenced by `src/coffee/editpost-widget/app.services.EditorAdapter.coffee`.
281
			 *
282
			 * @since 3.19.4
283
			 *
284
			 * @see https://github.com/insideout10/wordlift-plugin/issues/848
285
			 *
286
			 * @param string $editor The default editor id, by default `content`.
287
			 */
288
			'default_editor_id'          => apply_filters( 'wl_default_editor_id', 'content' ),
289
			/**
290
			 * Add the link to the Search Keywords admin page.
291
			 *
292
			 * @since 3.20.0
293
			 */
294
			'search_keywords_admin_page' => admin_url( 'admin.php?page=wl_configuration_admin_menu&tab=search-keywords' ),
295
		);
296
297
		// Set post-related values if there's a current post.
298
		if ( null !== $post = $entity_being_edited = get_post() ) {
299
300
			$params['post_id']           = $entity_being_edited->ID;
301
			$entity_service              = Wordlift_Entity_Service::get_instance();
302
			$params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() );
303
			// We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it
304
			// from the results, since we don't want the current entity to be discovered by the analysis.
305
			//
306
			// See https://github.com/insideout10/wordlift-plugin/issues/345
307
			$params['itemId']                      = $entity_service->get_uri( $entity_being_edited->ID );
308
			$params['wl_schemaorg_property_nonce'] = wp_create_nonce( 'wl_schemaorg_property' );
309
310
			/*
311
			 * Add the `properties` if `WL_ALL_ENTITY_TYPES` is enabled.
312
			 *
313
			 * @see https://github.com/insideout10/wordlift-plugin/issues/835
314
			 */
315
			if ( WL_ALL_ENTITY_TYPES ) {
316
				$params['properties'] = Wordlift_Schemaorg_Property_Service::get_instance()->get_all( $post->ID );
317
			}
318
319
		}
320
321
		// Finally output the params as `wlSettings` for JavaScript code.
322
		wp_localize_script( $this->plugin_name, 'wlSettings', apply_filters( 'wl_admin_settings', $params ) );
323
324
	}
325
326
	/**
327
	 * Require files needed for the Admin UI.
328
	 *
329
	 * @since 3.20.0
330
	 */
331
	private static function require_files() {
332
333
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-dashboard-latest-news.php';
334
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-search-rankings-service.php';
335
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-search-rankings-ajax-adapter.php';
336
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-dashboard-v2.php';
337
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-not-enriched-filter.php';
338
339
	}
340
341
	public static function is_gutenberg() {
342
		if ( function_exists( 'is_gutenberg_page' ) &&
343
		     is_gutenberg_page()
344
		) {
345
			// The Gutenberg plugin is on.
346
			return true;
347
		}
348
		$current_screen = get_current_screen();
349
		if ( method_exists( $current_screen, 'is_block_editor' ) &&
350
		     $current_screen->is_block_editor()
351
		) {
352
			// Gutenberg page on 5+.
353
			return true;
354
		}
355
356
		return false;
357
	}
358
359
}
360