Completed
Push — develop ( 6a9ba1...12b36e )
by David
09:36
created

Wordlift_Admin::__construct()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 9
nop 5
dl 0
loc 44
rs 8.9048
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 singleton instance.
63
	 *
64
	 * @since 3.20.0
65
	 * @access private
66
	 * @var \Wordlift_Admin The singleton instance.
67
	 */
68
	private static $instance;
69
70
	/**
71
	 * Initialize the class and set its properties.
72
	 *
73
	 * @since  1.0.0
74
	 *
75
	 * @param string                          $plugin_name The name of this plugin.
76
	 * @param string                          $version The version of this plugin.
77
	 * @param \Wordlift_Configuration_Service $configuration_service The configuration service.
78
	 * @param \Wordlift_Notice_Service        $notice_service The notice service.
79
	 * @param \Wordlift_User_Service          $user_service The {@link Wordlift_User_Service} instance.
80
	 */
81
	public function __construct( $plugin_name, $version, $configuration_service, $notice_service, $user_service ) {
82
83
		$this->plugin_name = $plugin_name;
84
		$this->version     = $version;
85
86
		$this->configuration_service = $configuration_service;
87
		$this->user_service          = $user_service;
88
89
		$dataset_uri = $configuration_service->get_dataset_uri();
90
		$key         = $configuration_service->get_key();
91
92
		if ( empty( $dataset_uri ) ) {
93
			$settings_page = Wordlift_Admin_Settings_Page::get_instance();
94
			if ( empty( $key ) ) {
95
				$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...
96
			} else {
97
				$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...
98
			}
99
			$notice_service->add_error( $error );
100
		}
101
102
		// Load additional code if we're in the admin UI.
103
		if ( is_admin() ) {
104
			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-dashboard-latest-news.php';
105
106
			new Wordlift_Dashboard_Latest_News();
107
108
			/*
109
			 * Add support for `All Entity Types`.
110
			 * @see https://github.com/insideout10/wordlift-plugin/issues/835
111
			 */
112
			if ( WL_ALL_ENTITY_TYPES ) {
113
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-schemaorg-taxonomy-metabox.php';
114
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wordlift-admin-schemaorg-property-metabox.php';
115
116
				new Wordlift_Admin_Schemaorg_Property_Metabox( Wordlift_Schemaorg_Property_Service::get_instance() );
117
			}
118
119
		}
120
121
		// Set the singleton instance.
122
		self::$instance = $this;
123
124
	}
125
126
	/**
127
	 * Get the singleton instance.
128
	 *
129
	 * @since 3.20.0
130
	 *
131
	 * @return \Wordlift_Admin The singleton instance.
132
	 */
133
	public static function get_instance() {
134
135
		return self::$instance;
136
	}
137
138
	/**
139
	 * Register the stylesheets for the admin area.
140
	 *
141
	 * @since    1.0.0
142
	 */
143
	public function enqueue_styles() {
144
145
		/**
146
		 * This function is provided for demonstration purposes only.
147
		 *
148
		 * An instance of this class should be passed to the run() function
149
		 * defined in Wordlift_Loader as all of the hooks are defined
150
		 * in that particular class.
151
		 *
152
		 * The Wordlift_Loader will then create the relationship
153
		 * between the defined hooks and the functions defined in this
154
		 * class.
155
		 */
156
157
		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' );
158
159
	}
160
161
	/**
162
	 * Register the JavaScript for the admin area.
163
	 *
164
	 * @since    1.0.0
165
	 */
166
	public function enqueue_scripts() {
167
168
		/**
169
		 * This function is provided for demonstration purposes only.
170
		 *
171
		 * An instance of this class should be passed to the run() function
172
		 * defined in Wordlift_Loader as all of the hooks are defined
173
		 * in that particular class.
174
		 *
175
		 * The Wordlift_Loader will then create the relationship
176
		 * between the defined Wordlift_Schemaorg_Property_Servicehooks and the functions defined in this
177
		 * class.
178
		 */
179
180
		// Enqueue the admin scripts.
181
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/1/admin.js', array(
182
			'jquery',
183
			'underscore',
184
			'backbone',
185
		), $this->version, false );
186
187
188
		$can_edit_wordlift_entities = current_user_can( 'edit_wordlift_entities' );
189
190
		/*
191
		 * People that can create entities will see the scope set in the wp-config.php file (by default `cloud`). People
192
		 * that cannot edit create entities will always see the local entities.
193
		 *
194
		 * @see https://github.com/insideout10/wordlift-plugin/issues/839
195
		 */
196
		$autocomplete_scope = $can_edit_wordlift_entities ? WL_AUTOCOMPLETE_SCOPE : "local";
197
198
		// Set the basic params.
199
		$params = array(
200
			// @todo scripts in admin should use wp.post.
201
			'ajax_url'              => admin_url( 'admin-ajax.php' ),
202
			// @todo remove specific actions from settings.
203
			'action'                => 'entity_by_title',
204
			'datasetUri'            => $this->configuration_service->get_dataset_uri(),
205
			'language'              => $this->configuration_service->get_language_code(),
206
			'link_by_default'       => $this->configuration_service->is_link_by_default(),
207
			// Whether the current user is allowed to create new entities.
208
			//
209
			// @see https://github.com/insideout10/wordlift-plugin/issues/561
210
			'can_create_entities'   => $can_edit_wordlift_entities ? 'yes' : 'no',
211
			'l10n'                  => array(
212
				'You already published an entity with the same name'                 => __( 'You already published an entity with the same name: ', 'wordlift' ),
213
				'logo_selection_title'                                               => __( 'WordLift Choose Logo', 'wordlift' ),
214
				'logo_selection_button'                                              => array( 'text' => __( 'Choose Logo', 'wordlift' ) ),
215
				'Type at least 3 characters to search...'                            => _x( 'Type at least 3 characters to search...', 'Autocomplete Select', 'wordlift' ),
216
				'No results found for your search.'                                  => _x( 'No results found: try changing or removing some words.', 'Autocomplete Select', 'wordlift' ),
217
				'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' ),
218
			),
219
			'wl_autocomplete_nonce' => wp_create_nonce( 'wordlift_autocomplete' ),
220
			'autocomplete_scope'    => $autocomplete_scope,
221
		);
222
223
		// Set post-related values if there's a current post.
224
		if ( null !== $post = $entity_being_edited = get_post() ) {
225
226
			$params['post_id']           = $entity_being_edited->ID;
227
			$entity_service              = Wordlift_Entity_Service::get_instance();
228
			$params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() );
229
			// We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it
230
			// from the results, since we don't want the current entity to be discovered by the analysis.
231
			//
232
			// See https://github.com/insideout10/wordlift-plugin/issues/345
233
			$params['itemId']                      = $entity_service->get_uri( $entity_being_edited->ID );
234
			$params['wl_schemaorg_property_nonce'] = wp_create_nonce( 'wl_schemaorg_property' );
235
236
			/*
237
			 * Add the `properties` if `WL_ALL_ENTITY_TYPES` is enabled.
238
			 *
239
			 * @see https://github.com/insideout10/wordlift-plugin/issues/835
240
			 */
241
			if ( WL_ALL_ENTITY_TYPES ) {
242
				$params['properties'] = Wordlift_Schemaorg_Property_Service::get_instance()->get_all( $post->ID );
243
			}
244
245
		}
246
247
		// Finally output the params as `wlSettings` for JavaScript code.
248
		wp_localize_script( $this->plugin_name, 'wlSettings', $params );
249
250
	}
251
252
}
253