Completed
Push — develop ( da4181...8a2a4a )
by Naveen
26s queued 10s
created

Wordlift_Admin::__construct()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 119

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 18
nop 5
dl 0
loc 119
rs 7.0666
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Wordlift\Mappings\Acf_Mappings;
14
use Wordlift\Mappings\Mappings_REST_Controller;
15
use Wordlift\Mappings\Mappings_Transform_Functions_Registry;
16
use Wordlift\Mappings\Pages\Admin_Mappings_Page;
17
use Wordlift\Mappings\Pages\Edit_Mappings_Page;
18
use Wordlift\Mappings\Taxonomy_Option;
19
20
/**
21
 * The admin-specific functionality of the plugin.
22
 *
23
 * Defines the plugin name, version, and two examples hooks for how to
24
 * enqueue the admin-specific stylesheet and JavaScript.
25
 *
26
 * @package    Wordlift
27
 * @subpackage Wordlift/admin
28
 * @author     WordLift <[email protected]>
29
 */
30
class Wordlift_Admin {
31
32
	/**
33
	 * The ID of this plugin.
34
	 *
35
	 * @since    1.0.0
36
	 * @access   private
37
	 * @var      string $plugin_name The ID of this plugin.
38
	 */
39
	private $plugin_name;
40
41
	/**
42
	 * The version of this plugin.
43
	 *
44
	 * @since    1.0.0
45
	 * @access   private
46
	 * @var      string $version The current version of this plugin.
47
	 */
48
	private $version;
49
50
	/**
51
	 * The {@link Wordlift_Configuration_Service} instance.
52
	 *
53
	 * @since  3.14.0
54
	 * @access private
55
	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
56
	 */
57
	private $configuration_service;
58
59
	/**
60
	 * The {@link Wordlift_User_Service} instance.
61
	 *
62
	 * @since  3.14.0
63
	 * @access private
64
	 * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance.
65
	 */
66
	private $user_service;
67
68
	/**
69
	 * The {@link Wordlift_Batch_Operation_Ajax_Adapter} instance.
70
	 *
71
	 * @since 3.20.0
72
	 * @access private
73
	 * @var \Wordlift_Batch_Operation_Ajax_Adapter $sync_batch_operation_ajax_adapter The {@link Wordlift_Batch_Operation_Ajax_Adapter} instance.
74
	 */
75
	private $sync_batch_operation_ajax_adapter;
76
77
	/**
78
	 * The singleton instance.
79
	 *
80
	 * @since 3.19.4
81
	 * @access private
82
	 * @var Wordlift_Admin $instance The singleton instance.
83
	 */
84
	private static $instance;
85
86
	/**
87
	 * Initialize the class and set its properties.
88
	 *
89
	 * @param string $plugin_name The name of this plugin.
90
	 * @param string $version The version of this plugin.
91
	 * @param \Wordlift_Configuration_Service $configuration_service The configuration service.
92
	 * @param \Wordlift_Notice_Service $notice_service The notice service.
93
	 * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance.
94
	 *
95
	 * @since  1.0.0
96
	 *
97
	 */
98
	public function __construct( $plugin_name, $version, $configuration_service, $notice_service, $user_service ) {
99
100
		$this->plugin_name = $plugin_name;
101
		$this->version     = $version;
102
103
		$this->configuration_service = $configuration_service;
104
		$this->user_service          = $user_service;
105
106
		$dataset_uri = $configuration_service->get_dataset_uri();
107
		$key         = $configuration_service->get_key();
108
109
		if ( empty( $dataset_uri ) ) {
110
			$settings_page = Wordlift_Admin_Settings_Page::get_instance();
111
			if ( empty( $key ) ) {
112
				$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...
113
			} else {
114
				$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...
115
			}
116
			$notice_service->add_error( $error );
117
		}
118
119
		// Load additional code if we're in the admin UI.
120
		if ( is_admin() ) {
121
122
			// Require the PHP files for the next code fragment.
123
			self::require_files();
124
125
			/*
126
			 * @since 3.24.2 This function isn't called anymore because it was causing the Block Category to
127
			 * multiply in Block Editor.
128
			 *
129
			 * @see https://github.com/insideout10/wordlift-plugin/issues/1004
130
			 */
131
			// Add Wordlift custom block category.
132
			// self::add_block_category();
133
134
			new Wordlift_Dashboard_Latest_News();
135
136
			// Search Rankings.
137
			$search_rankings_service = new Wordlift_Admin_Search_Rankings_Service( Wordlift_Api_Service::get_instance() );
138
			new Wordlift_Admin_Search_Rankings_Ajax_Adapter( $search_rankings_service );
139
140
			/*
141
			 * Add support for `All Entity Types`.
142
			 *
143
			 * @since 3.20.0
144
			 *
145
			 * @see https://github.com/insideout10/wordlift-plugin/issues/835
146
			 */
147
			if ( WL_ALL_ENTITY_TYPES ) {
148
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-schemaorg-taxonomy-metabox.php';
149
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-schemaorg-property-metabox.php';
150
151
				// new Wordlift_Admin_Schemaorg_Property_Metabox( Wordlift_Schemaorg_Property_Service::get_instance() );
152
				/*
153
				 * The `Mappings` admin page.
154
				 */
155
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-mappings-page.php';
156
				new Wordlift_Admin_Mappings_Page();
157
158
				/*
159
				 * Allow sync'ing the schema.org taxonomy with the schema.org json file.
160
				 *
161
				 * @since 3.20.0
162
				 */
163
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/schemaorg/class-wordlift-schemaorg-sync-batch-operation.php';
164
165
				$this->sync_batch_operation_ajax_adapter = new Wordlift_Batch_Operation_Ajax_Adapter( new Wordlift_Schemaorg_Sync_Batch_Operation(), 'wl_schemaorg_sync' );
166
167
			}
168
169
			/*
170
			 * Add the {@link Wordlift_Admin_Term_Adapter}.
171
			 *
172
			 * @since 3.20.0
173
			 */
174
			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-term-adapter.php';
175
			new Wordlift_Admin_Term_Adapter();
176
177
			/*
178
			 * The new dashboard.
179
			 *
180
			 * @since 3.20.0
181
			 *
182
			 * @see https://github.com/insideout10/wordlift-plugin/issues/879
183
			 */
184
			new Wordlift_Admin_Dashboard_V2(
185
				$search_rankings_service,
186
				Wordlift::get_instance()->get_dashboard_service(),
187
				Wordlift_Entity_Service::get_instance()
188
			);
189
			new Wordlift_Admin_Not_Enriched_Filter();
190
191
		}
192
193
		// @@todo only load this class if ACF is available.
194
		// Add support for ACF mappings, so that the admin edit mappings page can pick up ACF support when ACF is available.
195
		new Acf_Mappings();
196
197
		// Add the Mappings' REST Controller.
198
		new Mappings_REST_Controller();
199
200
		// Only enable the Mappings UI if the constant is defined.
201
		if ( defined( 'WL_ENABLE_MAPPINGS' ) && WL_ENABLE_MAPPINGS ) {
202
			// Add Mappings and Edit Mappings page.
203
			new Admin_Mappings_Page();
204
			/**
205
			 * @since 3.27.0
206
			 * Hooks in to ui of edit mapping screen, add taxonomy as a option.
207
			 */
208
			$taxonomy_option = new Taxonomy_Option();
209
			$taxonomy_option->add_taxonomy_option();
210
			new Edit_Mappings_Page( new Mappings_Transform_Functions_Registry() );
211
		}
212
213
		// Set the singleton instance.
214
		self::$instance = $this;
215
216
	}
217
218
	/**
219
	 * Get the singleton instance.
220
	 *
221
	 * @return \Wordlift_Admin The singleton instance.
222
	 * @since 3.19.4
223
	 *
224
	 */
225
	public static function get_instance() {
226
227
		return self::$instance;
228
	}
229
230
	/**
231
	 * Register the stylesheets for the admin area.
232
	 *
233
	 * @since    1.0.0
234
	 */
235
	public function enqueue_styles() {
236
237
		/**
238
		 * This function is provided for demonstration purposes only.
239
		 *
240
		 * An instance of this class should be passed to the run() function
241
		 * defined in Wordlift_Loader as all of the hooks are defined
242
		 * in that particular class.
243
		 *
244
		 * The Wordlift_Loader will then create the relationship
245
		 * between the defined hooks and the functions defined in this
246
		 * class.
247
		 */
248
249
		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' );
250
251
	}
252
253
	/**
254
	 * Register the JavaScript for the admin area.
255
	 *
256
	 * @since    1.0.0
257
	 */
258
	public function enqueue_scripts() {
259
260
		/*
261
		 * Do not load our scripts on the Filter Urls plugin admin pages.
262
		 *
263
		 * @see https://github.com/insideout10/wordlift-plugin/issues/901
264
		 * @since 3.20.0
265
		 */
266
		$screen = get_current_screen();
267
		if ( is_a( $screen, 'WP_Screen' ) && 'filter-urls_page_filter_urls_form' === $screen->id ) {
268
			return;
269
		}
270
271
		// Enqueue the admin scripts.
272
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/1/admin.js', array(
273
			'jquery',
274
			'underscore',
275
			'backbone',
276
		), $this->version, false );
277
278
279
		$params = $this->get_params();
280
281
		// Finally output the params as `wlSettings` for JavaScript code.
282
		wp_localize_script( $this->plugin_name, 'wlSettings', apply_filters( 'wl_admin_settings', $params ) );
283
284
	}
285
286
	/**
287
	 * Require files needed for the Admin UI.
288
	 *
289
	 * @since 3.20.0
290
	 */
291
	private static function require_files() {
292
293
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-dashboard-latest-news.php';
294
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-search-rankings-service.php';
295
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-search-rankings-ajax-adapter.php';
296
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-dashboard-v2.php';
297
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-not-enriched-filter.php';
298
299
	}
300
301 View Code Duplication
	public static function is_gutenberg() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
302
		if ( function_exists( 'is_gutenberg_page' ) &&
303
		     is_gutenberg_page()
304
		) {
305
			// The Gutenberg plugin is on.
306
			return true;
307
		}
308
		$current_screen = get_current_screen();
309
		if ( method_exists( $current_screen, 'is_block_editor' ) &&
310
		     $current_screen->is_block_editor()
311
		) {
312
			// Gutenberg page on 5+.
313
			return true;
314
		}
315
316
		return false;
317
	}
318
319
	/**
320
	 * Return the settings array by applying filters.
321
	 * @return array
322
	 */
323
	public function get_params() {
324
		$can_edit_wordlift_entities = current_user_can( 'edit_wordlift_entities' );
325
		/*
326
		 * People that can create entities will see the scope set in the wp-config.php file (by default `cloud`). People
327
		 * that cannot edit create entities will always see the local entities.
328
		 *
329
		 * @see https://github.com/insideout10/wordlift-plugin/issues/839
330
		 */
331
		$autocomplete_scope = $can_edit_wordlift_entities ? WL_AUTOCOMPLETE_SCOPE : "local";
332
333
		// Set the basic params.
334
		$params = array(
335
			// @todo scripts in admin should use wp.post.
336
			'ajax_url'                   => admin_url( 'admin-ajax.php' ),
337
			// @todo remove specific actions from settings.
338
			'action'                     => 'entity_by_title',
339
			'datasetUri'                 => $this->configuration_service->get_dataset_uri(),
340
			'language'                   => $this->configuration_service->get_language_code(),
341
			'link_by_default'            => $this->configuration_service->is_link_by_default(),
342
			// Whether the current user is allowed to create new entities.
343
			//
344
			// @see https://github.com/insideout10/wordlift-plugin/issues/561
345
			// @see https://github.com/insideout10/wordlift-plugin/issues/1267
346
			'can_create_entities'        => apply_filters( 'wl_features__enable__dataset', true ) ? ( $can_edit_wordlift_entities ? 'yes' : 'no' ) : 'no',
347
			'l10n'                       => array(
348
				'You already published an entity with the same name'                 => __( 'You already published an entity with the same name: ', 'wordlift' ),
349
				'logo_selection_title'                                               => __( 'WordLift Choose Logo', 'wordlift' ),
350
				'logo_selection_button'                                              => array( 'text' => __( 'Choose Logo', 'wordlift' ) ),
351
				'Type at least 3 characters to search...'                            => _x( 'Type at least 3 characters to search...', 'Autocomplete Select', 'wordlift' ),
352
				'No results found for your search.'                                  => _x( 'No results found: try changing or removing some words.', 'Autocomplete Select', 'wordlift' ),
353
				'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' ),
354
				'Add keywords to track'                                              => __( 'Add Keywords to track', 'wordlift' ),
355
			),
356
			'wl_autocomplete_nonce'      => wp_create_nonce( 'wl_autocomplete' ),
357
			'autocomplete_scope'         => $autocomplete_scope,
358
			/**
359
			 * Allow 3rd parties to define the default editor id. This turns useful if 3rd parties load
360
			 * or change the TinyMCE id.
361
			 *
362
			 * The editor id is currently referenced by `src/coffee/editpost-widget/app.services.EditorAdapter.coffee`.
363
			 *
364
			 * @param string $editor The default editor id, by default `content`.
365
			 *
366
			 * @see https://github.com/insideout10/wordlift-plugin/issues/848
367
			 *
368
			 * @since 3.19.4
369
			 *
370
			 */
371
			'default_editor_id'          => apply_filters( 'wl_default_editor_id', 'content' ),
372
			/**
373
			 * Add the link to the Search Keywords admin page.
374
			 *
375
			 * @since 3.20.0
376
			 */
377
			'search_keywords_admin_page' => admin_url( 'admin.php?page=wl_configuration_admin_menu&tab=search-keywords' ),
378
379
			'analysis'                     => array( '_wpnonce' => wp_create_nonce( 'wl_analyze' ) ),
380
			/**
381
			 * Faceted search default limit
382
			 *
383
			 * @since 3.26.1
384
			 */
385
			'faceted_search_default_limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ),
386
			/**
387
			 * WL Root path, to access in JS
388
			 *
389
			 * @since 3.27.3
390
			 */
391
			'wl_root'                      => plugin_dir_url( __DIR__ )
392
		);
393
394
		// Set post-related values if there's a current post.
395
		if ( null !== $post = $entity_being_edited = get_post() ) {
396
397
			$params['post_id']           = $entity_being_edited->ID;
398
			$entity_service              = Wordlift_Entity_Service::get_instance();
399
			$params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() );
400
			// We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it
401
			// from the results, since we don't want the current entity to be discovered by the analysis.
402
			//
403
			// See https://github.com/insideout10/wordlift-plugin/issues/345
404
			$params['itemId']                      = $entity_service->get_uri( $entity_being_edited->ID );
405
			$params['wl_schemaorg_property_nonce'] = wp_create_nonce( 'wl_schemaorg_property' );
406
407
			/*
408
			 * Add the `properties` if `WL_ALL_ENTITY_TYPES` is enabled.
409
			 *
410
			 * @see https://github.com/insideout10/wordlift-plugin/issues/835
411
			 */
412
			if ( WL_ALL_ENTITY_TYPES ) {
413
				$params['properties'] = Wordlift_Schemaorg_Property_Service::get_instance()->get_all( $post->ID );
414
			}
415
416
		}
417
418
		return $params;
419
	}
420
421
}
422