Completed
Push — master ( 37bdb3...5c343e )
by David
03:17 queued 10s
created

Wordlift_Admin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 160
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 3
A enqueue_styles() 0 17 1
B enqueue_scripts() 0 58 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
	 * Initialize the class and set its properties.
63
	 *
64
	 * @since  1.0.0
65
	 *
66
	 * @param string                          $plugin_name           The name of this plugin.
67
	 * @param string                          $version               The version of this plugin.
68
	 * @param \Wordlift_Configuration_Service $configuration_service The configuration service.
69
	 * @param \Wordlift_Notice_Service        $notice_service        The notice service.
70
	 * @param \Wordlift_User_Service          $user_service          The {@link Wordlift_User_Service} instance.
71
	 */
72
	public function __construct( $plugin_name, $version, $configuration_service, $notice_service, $user_service ) {
73
74
		$this->plugin_name = $plugin_name;
75
		$this->version     = $version;
76
77
		$this->configuration_service = $configuration_service;
78
		$this->user_service          = $user_service;
79
80
		$dataset_uri = $configuration_service->get_dataset_uri();
81
		$key         = $configuration_service->get_key();
82
83
		if ( empty( $dataset_uri ) ) {
84
			$settings_page = Wordlift_Admin_Settings_Page::get_instance();
85
			if ( empty( $key ) ) {
86
				$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...
87
			} else {
88
				$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...
89
			}
90
			$notice_service->add_error( $error );
91
		}
92
93
	}
94
95
	/**
96
	 * Register the stylesheets for the admin area.
97
	 *
98
	 * @since    1.0.0
99
	 */
100
	public function enqueue_styles() {
101
102
		/**
103
		 * This function is provided for demonstration purposes only.
104
		 *
105
		 * An instance of this class should be passed to the run() function
106
		 * defined in Wordlift_Loader as all of the hooks are defined
107
		 * in that particular class.
108
		 *
109
		 * The Wordlift_Loader will then create the relationship
110
		 * between the defined hooks and the functions defined in this
111
		 * class.
112
		 */
113
114
		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' );
115
116
	}
117
118
	/**
119
	 * Register the JavaScript for the admin area.
120
	 *
121
	 * @since    1.0.0
122
	 */
123
	public function enqueue_scripts() {
124
125
		/**
126
		 * This function is provided for demonstration purposes only.
127
		 *
128
		 * An instance of this class should be passed to the run() function
129
		 * defined in Wordlift_Loader as all of the hooks are defined
130
		 * in that particular class.
131
		 *
132
		 * The Wordlift_Loader will then create the relationship
133
		 * between the defined hooks and the functions defined in this
134
		 * class.
135
		 */
136
137
		// Enqueue the admin scripts.
138
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wordlift-admin.bundle.js', array(
139
			'jquery',
140
			'underscore',
141
			'backbone',
142
		), $this->version, false );
143
144
		// Set the basic params.
145
		$params = array(
146
			// @todo scripts in admin should use wp.post.
147
			'ajax_url'            => admin_url( 'admin-ajax.php' ),
148
			// @todo remove specific actions from settings.
149
			'action'              => 'entity_by_title',
150
			'datasetUri'          => $this->configuration_service->get_dataset_uri(),
151
			'language'            => $this->configuration_service->get_language_code(),
152
			'link_by_default'     => $this->configuration_service->is_link_by_default(),
153
			// Whether the current user is allowed to create new entities.
154
			//
155
			// @see https://github.com/insideout10/wordlift-plugin/issues/561
156
			'can_create_entities' => current_user_can( 'edit_wordlift_entities' ) ? 'yes' : 'no',
157
			'l10n'                => array(
158
				'You already published an entity with the same name' => __( 'You already published an entity with the same name: ', 'wordlift' ),
159
				'logo_selection_title'                               => __( 'WordLift Choose Logo', 'wordlift' ),
160
				'logo_selection_button'                              => array( 'text' => __( 'Choose Logo', 'wordlift' ) ),
161
			),
162
		);
163
164
		// Set post-related values if there's a current post.
165
		if ( null !== $post = $entity_being_edited = get_post() ) {
166
167
			$params['post_id']           = $entity_being_edited->ID;
168
			$params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && Wordlift_Entity_Service::TYPE_NAME == $entity_being_edited->post_type && is_numeric( get_the_ID() );
169
			// We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it
170
			// from the results, since we don't want the current entity to be discovered by the analysis.
171
			//
172
			// See https://github.com/insideout10/wordlift-plugin/issues/345
173
			$params['itemId'] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_being_edited->ID );
174
175
		}
176
177
		// Finally output the params as `wlSettings` for JavaScript code.
178
		wp_localize_script( $this->plugin_name, 'wlSettings', $params );
179
180
	}
181
182
}
183