Completed
Push — master ( 37bdb3...5c343e )
by David
03:17 queued 10s
created
src/admin/class-wordlift-admin-publisher-element.php 2 patches
Indentation   +114 added lines, -114 removed lines patch added patch discarded remove patch
@@ -19,116 +19,116 @@  discard block
 block discarded – undo
19 19
  */
20 20
 class Wordlift_Admin_Publisher_Element extends Wordlift_Admin_Author_Element {
21 21
 
22
-	/**
23
-	 * The {@link Wordlift_Configuration_Service} instance.
24
-	 *
25
-	 * @since  3.11.0
26
-	 * @access private
27
-	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
28
-	 */
29
-	private $configuration_service;
30
-
31
-	/**
32
-	 * The {@link Wordlift_Publisher_Service} instance.
33
-	 *
34
-	 * @since  3.11.0
35
-	 * @access private
36
-	 * @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance.
37
-	 */
38
-	private $publisher_service;
39
-
40
-	/**
41
-	 * @var Wordlift_Admin_Tabs_Element
42
-	 */
43
-	private $tabs_element;
44
-
45
-	/**
46
-	 * Create a {@link Wordlift_Admin_Publisher_Element} instance.
47
-	 *
48
-	 * @since 3.11.0
49
-	 *
50
-	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
51
-	 * @param \Wordlift_Publisher_Service     $publisher_service     The {@link Wordlift_Publisher_Service} instance.
52
-	 * @param \Wordlift_Admin_Tabs_Element    $tabs_element          The {@link Wordlift_Admin_Tabs_Element} instance.
53
-	 * @param \Wordlift_Admin_Select2_Element $select_element        The {@link Wordlift_Admin_Select_Element} instance.
54
-	 */
55
-	function __construct( $configuration_service, $publisher_service, $tabs_element, $select_element ) {
56
-		parent::__construct($publisher_service, $select_element);
57
-
58
-		$this->configuration_service = $configuration_service;
59
-		$this->publisher_service     = $publisher_service;
60
-
61
-		// Child elements.
62
-		$this->tabs_element   = $tabs_element;
63
-
64
-	}
65
-
66
-	/**
67
-	 * @inheritdoc
68
-	 */
69
-	public function render( $args ) {
70
-
71
-		// Parse the arguments and merge with default values.
72
-		$params = wp_parse_args( $args, array(
73
-			'id'   => uniqid( 'wl-input-' ),
74
-			'name' => uniqid( 'wl-input-' ),
75
-		) );
76
-
77
-		// Get the number of potential candidates as publishers.
78
-		$count = $this->publisher_service->count();
79
-
80
-		$this->tabs_element->render( array(
81
-			'tabs'   => array(
82
-				array(
83
-					'label'    => __( 'Select an Existing Publisher', 'wordlift' ),
84
-					'callback' => array( $this, 'select' ),
85
-					'args'     => $params,
86
-				),
87
-				array(
88
-					'label'    => __( 'Create a New Publisher', 'wordlift' ),
89
-					'callback' => array( $this, 'create' ),
90
-					'args'     => $params,
91
-				),
92
-			),
93
-			// Set the default tab according to the number of potential publishers
94
-			// configured in WP: 0 = select, 1 = create.
95
-			'active' => 0 === $count ? 1 : 0,
96
-		) );
97
-
98
-		// Finally return the element instance.
99
-		return $this;
100
-	}
101
-
102
-	/**
103
-	 * Render the publisher's select.
104
-	 *
105
-	 * @since 3.11.0
106
-	 *
107
-	 * @param array $params An array of parameters.
108
-	 */
109
-	public function select( $params ) {
110
-
111
-		// Get the configured publisher id. In case a publisher id is already configured
112
-		// this must be pre-loaded in the options.
113
-		$publisher_id = $this->configuration_service->get_publisher_id();
114
-
115
-		// Get the publisher data.
116
-		$data = $this->publisher_service->query();
117
-
118
-		// Call the select internal render.
119
-		$this->do_render( $params, $publisher_id, $data );
120
-
121
-	}
122
-
123
-	/**
124
-	 * Render the 'create publisher' form.
125
-	 *
126
-	 * @since 3.11.0
127
-	 *
128
-	 * @param array $params An array of parameters.
129
-	 */
130
-	public function create( $params ) {
131
-		?>
22
+    /**
23
+     * The {@link Wordlift_Configuration_Service} instance.
24
+     *
25
+     * @since  3.11.0
26
+     * @access private
27
+     * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
28
+     */
29
+    private $configuration_service;
30
+
31
+    /**
32
+     * The {@link Wordlift_Publisher_Service} instance.
33
+     *
34
+     * @since  3.11.0
35
+     * @access private
36
+     * @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance.
37
+     */
38
+    private $publisher_service;
39
+
40
+    /**
41
+     * @var Wordlift_Admin_Tabs_Element
42
+     */
43
+    private $tabs_element;
44
+
45
+    /**
46
+     * Create a {@link Wordlift_Admin_Publisher_Element} instance.
47
+     *
48
+     * @since 3.11.0
49
+     *
50
+     * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
51
+     * @param \Wordlift_Publisher_Service     $publisher_service     The {@link Wordlift_Publisher_Service} instance.
52
+     * @param \Wordlift_Admin_Tabs_Element    $tabs_element          The {@link Wordlift_Admin_Tabs_Element} instance.
53
+     * @param \Wordlift_Admin_Select2_Element $select_element        The {@link Wordlift_Admin_Select_Element} instance.
54
+     */
55
+    function __construct( $configuration_service, $publisher_service, $tabs_element, $select_element ) {
56
+        parent::__construct($publisher_service, $select_element);
57
+
58
+        $this->configuration_service = $configuration_service;
59
+        $this->publisher_service     = $publisher_service;
60
+
61
+        // Child elements.
62
+        $this->tabs_element   = $tabs_element;
63
+
64
+    }
65
+
66
+    /**
67
+     * @inheritdoc
68
+     */
69
+    public function render( $args ) {
70
+
71
+        // Parse the arguments and merge with default values.
72
+        $params = wp_parse_args( $args, array(
73
+            'id'   => uniqid( 'wl-input-' ),
74
+            'name' => uniqid( 'wl-input-' ),
75
+        ) );
76
+
77
+        // Get the number of potential candidates as publishers.
78
+        $count = $this->publisher_service->count();
79
+
80
+        $this->tabs_element->render( array(
81
+            'tabs'   => array(
82
+                array(
83
+                    'label'    => __( 'Select an Existing Publisher', 'wordlift' ),
84
+                    'callback' => array( $this, 'select' ),
85
+                    'args'     => $params,
86
+                ),
87
+                array(
88
+                    'label'    => __( 'Create a New Publisher', 'wordlift' ),
89
+                    'callback' => array( $this, 'create' ),
90
+                    'args'     => $params,
91
+                ),
92
+            ),
93
+            // Set the default tab according to the number of potential publishers
94
+            // configured in WP: 0 = select, 1 = create.
95
+            'active' => 0 === $count ? 1 : 0,
96
+        ) );
97
+
98
+        // Finally return the element instance.
99
+        return $this;
100
+    }
101
+
102
+    /**
103
+     * Render the publisher's select.
104
+     *
105
+     * @since 3.11.0
106
+     *
107
+     * @param array $params An array of parameters.
108
+     */
109
+    public function select( $params ) {
110
+
111
+        // Get the configured publisher id. In case a publisher id is already configured
112
+        // this must be pre-loaded in the options.
113
+        $publisher_id = $this->configuration_service->get_publisher_id();
114
+
115
+        // Get the publisher data.
116
+        $data = $this->publisher_service->query();
117
+
118
+        // Call the select internal render.
119
+        $this->do_render( $params, $publisher_id, $data );
120
+
121
+    }
122
+
123
+    /**
124
+     * Render the 'create publisher' form.
125
+     *
126
+     * @since 3.11.0
127
+     *
128
+     * @param array $params An array of parameters.
129
+     */
130
+    public function create( $params ) {
131
+        ?>
132 132
 		<p>
133 133
 			<strong><?php esc_html_e( 'Are you publishing as an individual or as a company?', 'wordlift' ) ?></strong>
134 134
 		</p>
@@ -138,13 +138,13 @@  discard block
 block discarded – undo
138 138
 				       name="wl_publisher[type]" value="person"
139 139
 				       checked="checked">
140 140
 				<label for="wl-publisher-person"><?php
141
-					esc_html_e( 'Person', 'wordlift' ) ?></label>
141
+                    esc_html_e( 'Person', 'wordlift' ) ?></label>
142 142
 			</span>
143 143
 			<span>
144 144
 				<input id="wl-publisher-company" type="radio"
145 145
 				       name="wl_publisher[type]" value="organization"">
146 146
 				<label for="wl-publisher-company"><?php
147
-					esc_html_e( 'Company', 'wordlift' ) ?></label>
147
+                    esc_html_e( 'Company', 'wordlift' ) ?></label>
148 148
 			</span>
149 149
 		</p>
150 150
 		<p id="wl-publisher-name">
@@ -161,10 +161,10 @@  discard block
 block discarded – undo
161 161
 				<img id="wl-publisher-media-uploader-preview" />
162 162
 				<button type="button" class="button"
163 163
 				        id="wl-publisher-media-uploader"><?php
164
-					esc_html_e( 'Select an existing image or upload a new one', 'wordlift' ); ?></button>
164
+                    esc_html_e( 'Select an existing image or upload a new one', 'wordlift' ); ?></button>
165 165
 			</p>
166 166
 		</div>
167 167
 		<?php
168
-	}
168
+    }
169 169
 
170 170
 }
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -52,48 +52,48 @@  discard block
 block discarded – undo
52 52
 	 * @param \Wordlift_Admin_Tabs_Element    $tabs_element          The {@link Wordlift_Admin_Tabs_Element} instance.
53 53
 	 * @param \Wordlift_Admin_Select2_Element $select_element        The {@link Wordlift_Admin_Select_Element} instance.
54 54
 	 */
55
-	function __construct( $configuration_service, $publisher_service, $tabs_element, $select_element ) {
55
+	function __construct($configuration_service, $publisher_service, $tabs_element, $select_element) {
56 56
 		parent::__construct($publisher_service, $select_element);
57 57
 
58 58
 		$this->configuration_service = $configuration_service;
59 59
 		$this->publisher_service     = $publisher_service;
60 60
 
61 61
 		// Child elements.
62
-		$this->tabs_element   = $tabs_element;
62
+		$this->tabs_element = $tabs_element;
63 63
 
64 64
 	}
65 65
 
66 66
 	/**
67 67
 	 * @inheritdoc
68 68
 	 */
69
-	public function render( $args ) {
69
+	public function render($args) {
70 70
 
71 71
 		// Parse the arguments and merge with default values.
72
-		$params = wp_parse_args( $args, array(
73
-			'id'   => uniqid( 'wl-input-' ),
74
-			'name' => uniqid( 'wl-input-' ),
75
-		) );
72
+		$params = wp_parse_args($args, array(
73
+			'id'   => uniqid('wl-input-'),
74
+			'name' => uniqid('wl-input-'),
75
+		));
76 76
 
77 77
 		// Get the number of potential candidates as publishers.
78 78
 		$count = $this->publisher_service->count();
79 79
 
80
-		$this->tabs_element->render( array(
80
+		$this->tabs_element->render(array(
81 81
 			'tabs'   => array(
82 82
 				array(
83
-					'label'    => __( 'Select an Existing Publisher', 'wordlift' ),
84
-					'callback' => array( $this, 'select' ),
83
+					'label'    => __('Select an Existing Publisher', 'wordlift'),
84
+					'callback' => array($this, 'select'),
85 85
 					'args'     => $params,
86 86
 				),
87 87
 				array(
88
-					'label'    => __( 'Create a New Publisher', 'wordlift' ),
89
-					'callback' => array( $this, 'create' ),
88
+					'label'    => __('Create a New Publisher', 'wordlift'),
89
+					'callback' => array($this, 'create'),
90 90
 					'args'     => $params,
91 91
 				),
92 92
 			),
93 93
 			// Set the default tab according to the number of potential publishers
94 94
 			// configured in WP: 0 = select, 1 = create.
95 95
 			'active' => 0 === $count ? 1 : 0,
96
-		) );
96
+		));
97 97
 
98 98
 		// Finally return the element instance.
99 99
 		return $this;
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
 	 *
107 107
 	 * @param array $params An array of parameters.
108 108
 	 */
109
-	public function select( $params ) {
109
+	public function select($params) {
110 110
 
111 111
 		// Get the configured publisher id. In case a publisher id is already configured
112 112
 		// this must be pre-loaded in the options.
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
 		$data = $this->publisher_service->query();
117 117
 
118 118
 		// Call the select internal render.
119
-		$this->do_render( $params, $publisher_id, $data );
119
+		$this->do_render($params, $publisher_id, $data);
120 120
 
121 121
 	}
122 122
 
@@ -127,10 +127,10 @@  discard block
 block discarded – undo
127 127
 	 *
128 128
 	 * @param array $params An array of parameters.
129 129
 	 */
130
-	public function create( $params ) {
130
+	public function create($params) {
131 131
 		?>
132 132
 		<p>
133
-			<strong><?php esc_html_e( 'Are you publishing as an individual or as a company?', 'wordlift' ) ?></strong>
133
+			<strong><?php esc_html_e('Are you publishing as an individual or as a company?', 'wordlift') ?></strong>
134 134
 		</p>
135 135
 		<p id="wl-publisher-type">
136 136
 			<span>
@@ -138,30 +138,30 @@  discard block
 block discarded – undo
138 138
 				       name="wl_publisher[type]" value="person"
139 139
 				       checked="checked">
140 140
 				<label for="wl-publisher-person"><?php
141
-					esc_html_e( 'Person', 'wordlift' ) ?></label>
141
+					esc_html_e('Person', 'wordlift') ?></label>
142 142
 			</span>
143 143
 			<span>
144 144
 				<input id="wl-publisher-company" type="radio"
145 145
 				       name="wl_publisher[type]" value="organization"">
146 146
 				<label for="wl-publisher-company"><?php
147
-					esc_html_e( 'Company', 'wordlift' ) ?></label>
147
+					esc_html_e('Company', 'wordlift') ?></label>
148 148
 			</span>
149 149
 		</p>
150 150
 		<p id="wl-publisher-name">
151 151
 			<input type="text" name="wl_publisher[name]"
152
-			       placeholder="<?php echo esc_attr__( "What's your name?", 'wordlift' ) ?>">
152
+			       placeholder="<?php echo esc_attr__("What's your name?", 'wordlift') ?>">
153 153
 		</p>
154 154
 		<div id="wl-publisher-logo">
155 155
 			<input type="hidden" id="wl-publisher-media-uploader-id"
156 156
 			       name="wl_publisher[thumbnail_id]" />
157 157
 			<p>
158
-				<b><?php esc_html_e( "Choose the publisher's Logo", 'wordlift' ) ?></b>
158
+				<b><?php esc_html_e("Choose the publisher's Logo", 'wordlift') ?></b>
159 159
 			</p>
160 160
 			<p>
161 161
 				<img id="wl-publisher-media-uploader-preview" />
162 162
 				<button type="button" class="button"
163 163
 				        id="wl-publisher-media-uploader"><?php
164
-					esc_html_e( 'Select an existing image or upload a new one', 'wordlift' ); ?></button>
164
+					esc_html_e('Select an existing image or upload a new one', 'wordlift'); ?></button>
165 165
 			</p>
166 166
 		</div>
167 167
 		<?php
Please login to merge, or discard this patch.
src/admin/class-wordlift-admin-author-element.php 2 patches
Indentation   +107 added lines, -107 removed lines patch added patch discarded remove patch
@@ -19,112 +19,112 @@
 block discarded – undo
19 19
  */
20 20
 class Wordlift_Admin_Author_Element implements Wordlift_Admin_Element {
21 21
 
22
-	/**
23
-	 * The {@link Wordlift_Publisher_Service} instance.
24
-	 *
25
-	 * @since  3.14.0
26
-	 * @access private
27
-	 * @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance.
28
-	 */
29
-	private $publisher_service;
30
-
31
-	/**
32
-	 * A {@link Wordlift_Admin_Select2_Element} instance.
33
-	 *
34
-	 * @since  3.14.0
35
-	 * @access private
36
-	 * @var \Wordlift_Admin_Select2_Element $select_element A {@link Wordlift_Admin_Select2_Element} instance.
37
-	 */
38
-	private $select_element;
39
-
40
-	/**
41
-	 * Create a {@link Wordlift_Admin_Person_Element} instance.
42
-	 *
43
-	 * @since 3.14.0
44
-	 *
45
-	 * @param \Wordlift_Publisher_Service     $publisher_service The {@link Wordlift_Publisher_Service} instance.
46
-	 * @param \Wordlift_Admin_Select2_Element $select_element    The {@link Wordlift_Admin_Select_Element} instance.
47
-	 */
48
-	function __construct( $publisher_service, $select_element ) {
49
-
50
-		$this->publisher_service = $publisher_service;
51
-
52
-		// Child elements.
53
-		$this->select_element = $select_element;
54
-
55
-	}
56
-
57
-	/**
58
-	 * @inheritdoc
59
-	 */
60
-	public function render( $args ) {
61
-
62
-		// Parse the arguments and merge with default values.
63
-		$params = wp_parse_args( $args, array(
64
-			'id'             => uniqid( 'wl-input-' ),
65
-			'name'           => uniqid( 'wl-input-' ),
66
-			'current_entity' => 0,
67
-		) );
68
-
69
-		$current_entity_id = $params['current_entity'];
70
-		$data              = $this->publisher_service->query();
71
-
72
-		// Set a default to show when no entity is associated and a way to unassign.
73
-		array_unshift( $data, array(
74
-			'id'            => '0',
75
-			'text'          => __( '<em>(none)</em>', 'wordlift' ),
76
-			'type'          => '',
77
-			'thumbnail_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'images/pixel.png',
78
-		) );
79
-
80
-		// Finally do the render, passing along also the current selected entity
81
-		// id and the options data.
82
-		return $this->do_render( $params, $current_entity_id, $data );
83
-	}
84
-
85
-	/**
86
-	 * Render the `select` using the provided parameters.
87
-	 *
88
-	 * @since 3.14.0
89
-	 *
90
-	 * @param array $params          The array of parameters from the `render` function.
91
-	 * @param int   $current_post_id The currently selected {@link WP_Post} `id`.
92
-	 * @param array $data            An array of Select2 options.
93
-	 *
94
-	 * @return \Wordlift_Admin_Author_Element $this Return this element.
95
-	 */
96
-	protected function do_render( $params, $current_post_id, $data ) {
97
-
98
-		// Queue the script which will initialize the select and style it.
99
-		wp_enqueue_script( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift-author-element.bundle.js', array( 'wordlift-select2' ) );
100
-
101
-		// Prepare the URLs for entities which don't have logos.
102
-		$person_thumbnail_url       = plugin_dir_url( dirname( __FILE__ ) ) . 'images/person.png';
103
-		$organization_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/organization.png';
104
-
105
-		// Get the current post.
106
-		$current_post = $current_post_id ? get_post( $current_post_id ) : null;
107
-
108
-		// Finally render the Select.
109
-		$this->select_element->render( array(
110
-			// Id.
111
-			'id'                 => $params['id'],
112
-			// Name.
113
-			'name'               => $params['name'],
114
-			// The selected id.
115
-			'value'              => $current_post_id,
116
-			// The selected item (must be in the options for Select2 to display it).
117
-			'options'            => $current_post ? array( $current_post->ID => $current_post->post_title ) : array(),
118
-			// The list of available options.
119
-			'data'               => $data,
120
-			// The HTML template for each option.
121
-			'template-result'    => "<div class='wl-select2-result'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'>&nbsp;</span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>",
122
-			// The HTML template for the selected option.
123
-			'template-selection' => "<div class='wl-select2-selection'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'>&nbsp;</span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>",
124
-		) );
125
-
126
-		// Finally return the element instance.
127
-		return $this;
128
-	}
22
+    /**
23
+     * The {@link Wordlift_Publisher_Service} instance.
24
+     *
25
+     * @since  3.14.0
26
+     * @access private
27
+     * @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance.
28
+     */
29
+    private $publisher_service;
30
+
31
+    /**
32
+     * A {@link Wordlift_Admin_Select2_Element} instance.
33
+     *
34
+     * @since  3.14.0
35
+     * @access private
36
+     * @var \Wordlift_Admin_Select2_Element $select_element A {@link Wordlift_Admin_Select2_Element} instance.
37
+     */
38
+    private $select_element;
39
+
40
+    /**
41
+     * Create a {@link Wordlift_Admin_Person_Element} instance.
42
+     *
43
+     * @since 3.14.0
44
+     *
45
+     * @param \Wordlift_Publisher_Service     $publisher_service The {@link Wordlift_Publisher_Service} instance.
46
+     * @param \Wordlift_Admin_Select2_Element $select_element    The {@link Wordlift_Admin_Select_Element} instance.
47
+     */
48
+    function __construct( $publisher_service, $select_element ) {
49
+
50
+        $this->publisher_service = $publisher_service;
51
+
52
+        // Child elements.
53
+        $this->select_element = $select_element;
54
+
55
+    }
56
+
57
+    /**
58
+     * @inheritdoc
59
+     */
60
+    public function render( $args ) {
61
+
62
+        // Parse the arguments and merge with default values.
63
+        $params = wp_parse_args( $args, array(
64
+            'id'             => uniqid( 'wl-input-' ),
65
+            'name'           => uniqid( 'wl-input-' ),
66
+            'current_entity' => 0,
67
+        ) );
68
+
69
+        $current_entity_id = $params['current_entity'];
70
+        $data              = $this->publisher_service->query();
71
+
72
+        // Set a default to show when no entity is associated and a way to unassign.
73
+        array_unshift( $data, array(
74
+            'id'            => '0',
75
+            'text'          => __( '<em>(none)</em>', 'wordlift' ),
76
+            'type'          => '',
77
+            'thumbnail_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'images/pixel.png',
78
+        ) );
79
+
80
+        // Finally do the render, passing along also the current selected entity
81
+        // id and the options data.
82
+        return $this->do_render( $params, $current_entity_id, $data );
83
+    }
84
+
85
+    /**
86
+     * Render the `select` using the provided parameters.
87
+     *
88
+     * @since 3.14.0
89
+     *
90
+     * @param array $params          The array of parameters from the `render` function.
91
+     * @param int   $current_post_id The currently selected {@link WP_Post} `id`.
92
+     * @param array $data            An array of Select2 options.
93
+     *
94
+     * @return \Wordlift_Admin_Author_Element $this Return this element.
95
+     */
96
+    protected function do_render( $params, $current_post_id, $data ) {
97
+
98
+        // Queue the script which will initialize the select and style it.
99
+        wp_enqueue_script( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift-author-element.bundle.js', array( 'wordlift-select2' ) );
100
+
101
+        // Prepare the URLs for entities which don't have logos.
102
+        $person_thumbnail_url       = plugin_dir_url( dirname( __FILE__ ) ) . 'images/person.png';
103
+        $organization_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/organization.png';
104
+
105
+        // Get the current post.
106
+        $current_post = $current_post_id ? get_post( $current_post_id ) : null;
107
+
108
+        // Finally render the Select.
109
+        $this->select_element->render( array(
110
+            // Id.
111
+            'id'                 => $params['id'],
112
+            // Name.
113
+            'name'               => $params['name'],
114
+            // The selected id.
115
+            'value'              => $current_post_id,
116
+            // The selected item (must be in the options for Select2 to display it).
117
+            'options'            => $current_post ? array( $current_post->ID => $current_post->post_title ) : array(),
118
+            // The list of available options.
119
+            'data'               => $data,
120
+            // The HTML template for each option.
121
+            'template-result'    => "<div class='wl-select2-result'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'>&nbsp;</span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>",
122
+            // The HTML template for the selected option.
123
+            'template-selection' => "<div class='wl-select2-selection'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'>&nbsp;</span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>",
124
+        ) );
125
+
126
+        // Finally return the element instance.
127
+        return $this;
128
+    }
129 129
 
130 130
 }
Please login to merge, or discard this patch.
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
 	 * @param \Wordlift_Publisher_Service     $publisher_service The {@link Wordlift_Publisher_Service} instance.
46 46
 	 * @param \Wordlift_Admin_Select2_Element $select_element    The {@link Wordlift_Admin_Select_Element} instance.
47 47
 	 */
48
-	function __construct( $publisher_service, $select_element ) {
48
+	function __construct($publisher_service, $select_element) {
49 49
 
50 50
 		$this->publisher_service = $publisher_service;
51 51
 
@@ -57,29 +57,29 @@  discard block
 block discarded – undo
57 57
 	/**
58 58
 	 * @inheritdoc
59 59
 	 */
60
-	public function render( $args ) {
60
+	public function render($args) {
61 61
 
62 62
 		// Parse the arguments and merge with default values.
63
-		$params = wp_parse_args( $args, array(
64
-			'id'             => uniqid( 'wl-input-' ),
65
-			'name'           => uniqid( 'wl-input-' ),
63
+		$params = wp_parse_args($args, array(
64
+			'id'             => uniqid('wl-input-'),
65
+			'name'           => uniqid('wl-input-'),
66 66
 			'current_entity' => 0,
67
-		) );
67
+		));
68 68
 
69 69
 		$current_entity_id = $params['current_entity'];
70 70
 		$data              = $this->publisher_service->query();
71 71
 
72 72
 		// Set a default to show when no entity is associated and a way to unassign.
73
-		array_unshift( $data, array(
73
+		array_unshift($data, array(
74 74
 			'id'            => '0',
75
-			'text'          => __( '<em>(none)</em>', 'wordlift' ),
75
+			'text'          => __('<em>(none)</em>', 'wordlift'),
76 76
 			'type'          => '',
77
-			'thumbnail_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'images/pixel.png',
78
-		) );
77
+			'thumbnail_url' => plugin_dir_url(dirname(__FILE__)).'images/pixel.png',
78
+		));
79 79
 
80 80
 		// Finally do the render, passing along also the current selected entity
81 81
 		// id and the options data.
82
-		return $this->do_render( $params, $current_entity_id, $data );
82
+		return $this->do_render($params, $current_entity_id, $data);
83 83
 	}
84 84
 
85 85
 	/**
@@ -93,20 +93,20 @@  discard block
 block discarded – undo
93 93
 	 *
94 94
 	 * @return \Wordlift_Admin_Author_Element $this Return this element.
95 95
 	 */
96
-	protected function do_render( $params, $current_post_id, $data ) {
96
+	protected function do_render($params, $current_post_id, $data) {
97 97
 
98 98
 		// Queue the script which will initialize the select and style it.
99
-		wp_enqueue_script( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift-author-element.bundle.js', array( 'wordlift-select2' ) );
99
+		wp_enqueue_script('wl-author-element', plugin_dir_url(dirname(__FILE__)).'admin/js/wordlift-author-element.bundle.js', array('wordlift-select2'));
100 100
 
101 101
 		// Prepare the URLs for entities which don't have logos.
102
-		$person_thumbnail_url       = plugin_dir_url( dirname( __FILE__ ) ) . 'images/person.png';
103
-		$organization_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/organization.png';
102
+		$person_thumbnail_url       = plugin_dir_url(dirname(__FILE__)).'images/person.png';
103
+		$organization_thumbnail_url = plugin_dir_url(dirname(__FILE__)).'images/organization.png';
104 104
 
105 105
 		// Get the current post.
106
-		$current_post = $current_post_id ? get_post( $current_post_id ) : null;
106
+		$current_post = $current_post_id ? get_post($current_post_id) : null;
107 107
 
108 108
 		// Finally render the Select.
109
-		$this->select_element->render( array(
109
+		$this->select_element->render(array(
110 110
 			// Id.
111 111
 			'id'                 => $params['id'],
112 112
 			// Name.
@@ -114,14 +114,14 @@  discard block
 block discarded – undo
114 114
 			// The selected id.
115 115
 			'value'              => $current_post_id,
116 116
 			// The selected item (must be in the options for Select2 to display it).
117
-			'options'            => $current_post ? array( $current_post->ID => $current_post->post_title ) : array(),
117
+			'options'            => $current_post ? array($current_post->ID => $current_post->post_title) : array(),
118 118
 			// The list of available options.
119 119
 			'data'               => $data,
120 120
 			// The HTML template for each option.
121 121
 			'template-result'    => "<div class='wl-select2-result'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'>&nbsp;</span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>",
122 122
 			// The HTML template for the selected option.
123 123
 			'template-selection' => "<div class='wl-select2-selection'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'>&nbsp;</span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>",
124
-		) );
124
+		));
125 125
 
126 126
 		// Finally return the element instance.
127 127
 		return $this;
Please login to merge, or discard this patch.
src/includes/class-wordlift-activator.php 2 patches
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -32,17 +32,17 @@  discard block
 block discarded – undo
32 32
 	public static function activate() {
33 33
 
34 34
 		// Do not let the plugin be activate on wordpress versions before 4.2.
35
-		$version = get_bloginfo( 'version' );
36
-		if ( version_compare( $version, '4.2', '<' ) ) {
37
-			die( __( 'The WordLift plugin requires WordPress version 4.2 or above.', 'wordlift' ) );
35
+		$version = get_bloginfo('version');
36
+		if (version_compare($version, '4.2', '<')) {
37
+			die(__('The WordLift plugin requires WordPress version 4.2 or above.', 'wordlift'));
38 38
 		}
39 39
 
40 40
 		$configuration_service = Wordlift_Configuration_Service::get_instance();
41 41
 
42 42
 		// Create a blank application key if there is none
43 43
 		$key = $configuration_service->get_key();
44
-		if ( empty( $key ) ) {
45
-			$configuration_service->set_key( '' );
44
+		if (empty($key)) {
45
+			$configuration_service->set_key('');
46 46
 		}
47 47
 
48 48
 		// Intentionally go through the whole upgrade procedure to be DRY.
@@ -50,8 +50,8 @@  discard block
 block discarded – undo
50 50
 
51 51
 		// If WordLift's key is not configured, set `_wl_activation_redirect` transient. We won't redirect here, because we can't give
52 52
 		// for granted that we're in a browser admin session.
53
-		if ( '' === $configuration_service->get_key() ) {
54
-			set_transient( '_wl_activation_redirect', true, 30 );
53
+		if ('' === $configuration_service->get_key()) {
54
+			set_transient('_wl_activation_redirect', true, 30);
55 55
 		}
56 56
 	}
57 57
 
Please login to merge, or discard this patch.
Indentation   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -22,38 +22,38 @@
 block discarded – undo
22 22
  */
23 23
 class Wordlift_Activator {
24 24
 
25
-	/**
26
-	 * Short Description. (use period)
27
-	 *
28
-	 * Long Description.
29
-	 *
30
-	 * @since    1.0.0
31
-	 */
32
-	public static function activate() {
33
-
34
-		// Do not let the plugin be activate on wordpress versions before 4.2.
35
-		$version = get_bloginfo( 'version' );
36
-		if ( version_compare( $version, '4.2', '<' ) ) {
37
-			die( __( 'The WordLift plugin requires WordPress version 4.2 or above.', 'wordlift' ) );
38
-		}
39
-
40
-		$configuration_service = Wordlift_Configuration_Service::get_instance();
41
-
42
-		// Create a blank application key if there is none
43
-		$key = $configuration_service->get_key();
44
-		if ( empty( $key ) ) {
45
-			$configuration_service->set_key( '' );
46
-		}
47
-
48
-		// Intentionally go through the whole upgrade procedure to be DRY.
49
-		wl_core_update_db_check();
50
-
51
-		// If WordLift's key is not configured, set `_wl_activation_redirect` transient. We won't redirect here, because we can't give
52
-		// for granted that we're in a browser admin session.
53
-		if ( '' === $configuration_service->get_key() ) {
54
-			set_transient( '_wl_activation_redirect', true, 30 );
55
-		}
56
-
57
-	}
25
+    /**
26
+     * Short Description. (use period)
27
+     *
28
+     * Long Description.
29
+     *
30
+     * @since    1.0.0
31
+     */
32
+    public static function activate() {
33
+
34
+        // Do not let the plugin be activate on wordpress versions before 4.2.
35
+        $version = get_bloginfo( 'version' );
36
+        if ( version_compare( $version, '4.2', '<' ) ) {
37
+            die( __( 'The WordLift plugin requires WordPress version 4.2 or above.', 'wordlift' ) );
38
+        }
39
+
40
+        $configuration_service = Wordlift_Configuration_Service::get_instance();
41
+
42
+        // Create a blank application key if there is none
43
+        $key = $configuration_service->get_key();
44
+        if ( empty( $key ) ) {
45
+            $configuration_service->set_key( '' );
46
+        }
47
+
48
+        // Intentionally go through the whole upgrade procedure to be DRY.
49
+        wl_core_update_db_check();
50
+
51
+        // If WordLift's key is not configured, set `_wl_activation_redirect` transient. We won't redirect here, because we can't give
52
+        // for granted that we're in a browser admin session.
53
+        if ( '' === $configuration_service->get_key() ) {
54
+            set_transient( '_wl_activation_redirect', true, 30 );
55
+        }
56
+
57
+    }
58 58
 
59 59
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-post-adapter.php 2 patches
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -16,42 +16,42 @@
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Post_Adapter {
18 18
 
19
-	/**
20
-	 * The post id to which the adopter relates.
21
-	 *
22
-	 * @since 3.14.0
23
-	 *
24
-	 * @var integer $post_id .
25
-	 */
26
-	private $post_id;
27
-
28
-	/**
29
-	 * Create the {@link Wordlift_Post_Adatpter} instance.
30
-	 *
31
-	 * @since 3.14.0
32
-	 *
33
-	 * @param integer $post_id the post ID of the post the adopter relates to.
34
-	 */
35
-	function __construct( $post_id ) {
36
-
37
-		$this->post_id = $post_id;
38
-
39
-	}
40
-
41
-	/**
42
-	 * Get the word count of the post content.
43
-	 *
44
-	 * The count is calculated over the post content after stripping shortcodes and html tags.
45
-	 *
46
-	 * @since 3.14.0
47
-	 *
48
-	 * @return integer the number of words in the content after stripping shortcodes and html tags..
49
-	 */
50
-	public function word_count() {
51
-
52
-		$post = get_post( $this->post_id );
53
-
54
-		return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) );
55
-	}
19
+    /**
20
+     * The post id to which the adopter relates.
21
+     *
22
+     * @since 3.14.0
23
+     *
24
+     * @var integer $post_id .
25
+     */
26
+    private $post_id;
27
+
28
+    /**
29
+     * Create the {@link Wordlift_Post_Adatpter} instance.
30
+     *
31
+     * @since 3.14.0
32
+     *
33
+     * @param integer $post_id the post ID of the post the adopter relates to.
34
+     */
35
+    function __construct( $post_id ) {
36
+
37
+        $this->post_id = $post_id;
38
+
39
+    }
40
+
41
+    /**
42
+     * Get the word count of the post content.
43
+     *
44
+     * The count is calculated over the post content after stripping shortcodes and html tags.
45
+     *
46
+     * @since 3.14.0
47
+     *
48
+     * @return integer the number of words in the content after stripping shortcodes and html tags..
49
+     */
50
+    public function word_count() {
51
+
52
+        $post = get_post( $this->post_id );
53
+
54
+        return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) );
55
+    }
56 56
 
57 57
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -32,7 +32,7 @@  discard block
 block discarded – undo
32 32
 	 *
33 33
 	 * @param integer $post_id the post ID of the post the adopter relates to.
34 34
 	 */
35
-	function __construct( $post_id ) {
35
+	function __construct($post_id) {
36 36
 
37 37
 		$this->post_id = $post_id;
38 38
 
@@ -49,9 +49,9 @@  discard block
 block discarded – undo
49 49
 	 */
50 50
 	public function word_count() {
51 51
 
52
-		$post = get_post( $this->post_id );
52
+		$post = get_post($this->post_id);
53 53
 
54
-		return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) );
54
+		return str_word_count(strip_tags(strip_shortcodes($post->post_content)));
55 55
 	}
56 56
 
57 57
 }
Please login to merge, or discard this patch.
src/admin/partials/wordlift-admin-setup.php 2 patches
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -16,38 +16,38 @@  discard block
 block discarded – undo
16 16
 
17 17
 	<?php
18 18
 
19
-	// Enqueue wp.media functions.
20
-	wp_enqueue_media();
21
-
22
-	// Enqueue styles and scripts.
23
-	wp_enqueue_style( 'wl-font-awesome', plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'css/font-awesome.min.css' );
24
-	wp_enqueue_style( 'wordlift-admin-setup', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-admin-setup.css', array(
25
-		'wp-admin',
26
-		'wl-font-awesome',
27
-	) );
28
-	wp_enqueue_script( 'wordlift-admin-setup', plugin_dir_url( dirname( __FILE__ ) ) . 'js/wordlift-admin-setup.js', array( 'jquery' ) );
29
-
30
-	//Get wp_permalink structure
31
-	$permalink_structure = get_option( 'permalink_structure' );
19
+    // Enqueue wp.media functions.
20
+    wp_enqueue_media();
21
+
22
+    // Enqueue styles and scripts.
23
+    wp_enqueue_style( 'wl-font-awesome', plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'css/font-awesome.min.css' );
24
+    wp_enqueue_style( 'wordlift-admin-setup', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-admin-setup.css', array(
25
+        'wp-admin',
26
+        'wl-font-awesome',
27
+    ) );
28
+    wp_enqueue_script( 'wordlift-admin-setup', plugin_dir_url( dirname( __FILE__ ) ) . 'js/wordlift-admin-setup.js', array( 'jquery' ) );
29
+
30
+    //Get wp_permalink structure
31
+    $permalink_structure = get_option( 'permalink_structure' );
32 32
 	
33
-	// Set configuration settings.
34
-	wp_localize_script( 'wordlift-admin-setup', '_wlAdminSetup', array(
35
-		'ajaxUrl'   => parse_url( self_admin_url( 'admin-ajax.php' ), PHP_URL_PATH ),
36
-		'action'    => 'wl_validate_key',
37
-		'permalink' => $permalink_structure,
38
-		'media'     => array(
39
-			'title'  => __( 'WordLift Choose Logo', 'wordlift' ),
40
-			'button' => array( 'text' => __( 'Choose Logo', 'wordlift' ) ),
41
-		),
42
-	) );
43
-
44
-	// Finally print styles and scripts.
45
-	wp_print_styles();
46
-	wp_print_scripts();
47
-	//	do_action( 'admin_print_styles' );
48
-	//	do_action( 'admin_print_scripts' );
49
-
50
-	?>
33
+    // Set configuration settings.
34
+    wp_localize_script( 'wordlift-admin-setup', '_wlAdminSetup', array(
35
+        'ajaxUrl'   => parse_url( self_admin_url( 'admin-ajax.php' ), PHP_URL_PATH ),
36
+        'action'    => 'wl_validate_key',
37
+        'permalink' => $permalink_structure,
38
+        'media'     => array(
39
+            'title'  => __( 'WordLift Choose Logo', 'wordlift' ),
40
+            'button' => array( 'text' => __( 'Choose Logo', 'wordlift' ) ),
41
+        ),
42
+    ) );
43
+
44
+    // Finally print styles and scripts.
45
+    wp_print_styles();
46
+    wp_print_scripts();
47
+    //	do_action( 'admin_print_styles' );
48
+    //	do_action( 'admin_print_scripts' );
49
+
50
+    ?>
51 51
 
52 52
 	<!-- Pane 1 content -->
53 53
 	<script type="text/html" id="page-0">
@@ -144,20 +144,20 @@  discard block
 block discarded – undo
144 144
 		        placeholder="<?php esc_attr_e( 'Choose your language', 'wordlift' ); ?>">
145 145
 			<?php
146 146
 
147
-			// Get WordLift's supported languages.
148
-			$languages = Wordlift_Languages::get_languages();
147
+            // Get WordLift's supported languages.
148
+            $languages = Wordlift_Languages::get_languages();
149 149
 
150
-			// Get WP's locale.
151
-			$locale = get_locale();
150
+            // Get WP's locale.
151
+            $locale = get_locale();
152 152
 
153
-			// Get the language locale part.
154
-			$parts = explode( '_', $locale );
153
+            // Get the language locale part.
154
+            $parts = explode( '_', $locale );
155 155
 
156
-			// If we support WP's configured language, then use that, otherwise use English by default.
157
-			$language = isset( $languages[ $parts[0] ] ) ? $parts[0] : 'en';
156
+            // If we support WP's configured language, then use that, otherwise use English by default.
157
+            $language = isset( $languages[ $parts[0] ] ) ? $parts[0] : 'en';
158 158
 
159
-			// Print all the supported language, preselecting the one configured in WP (or English if not supported).
160
-			foreach ( $languages as $code => $label ) { ?>
159
+            // Print all the supported language, preselecting the one configured in WP (or English if not supported).
160
+            foreach ( $languages as $code => $label ) { ?>
161 161
 				<option
162 162
 					value="<?php echo esc_attr( $code ) ?>" <?php echo selected( $code, $language, false ) ?>><?php echo esc_html( $label ) ?></option>
163 163
 			<?php } ?>
Please login to merge, or discard this patch.
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
 	<!-- Defining responsive ambient. -->
13 13
 	<meta name="viewport" content="width=device-width, initial-scale=1">
14 14
 
15
-	<title><?php esc_html_e( 'WordLift Setup', 'wordlift' ); ?></title>
15
+	<title><?php esc_html_e('WordLift Setup', 'wordlift'); ?></title>
16 16
 
17 17
 	<?php
18 18
 
@@ -20,26 +20,26 @@  discard block
 block discarded – undo
20 20
 	wp_enqueue_media();
21 21
 
22 22
 	// Enqueue styles and scripts.
23
-	wp_enqueue_style( 'wl-font-awesome', plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'css/font-awesome.min.css' );
24
-	wp_enqueue_style( 'wordlift-admin-setup', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-admin-setup.css', array(
23
+	wp_enqueue_style('wl-font-awesome', plugin_dir_url(dirname(dirname(__FILE__))).'css/font-awesome.min.css');
24
+	wp_enqueue_style('wordlift-admin-setup', plugin_dir_url(dirname(__FILE__)).'css/wordlift-admin-setup.css', array(
25 25
 		'wp-admin',
26 26
 		'wl-font-awesome',
27
-	) );
28
-	wp_enqueue_script( 'wordlift-admin-setup', plugin_dir_url( dirname( __FILE__ ) ) . 'js/wordlift-admin-setup.js', array( 'jquery' ) );
27
+	));
28
+	wp_enqueue_script('wordlift-admin-setup', plugin_dir_url(dirname(__FILE__)).'js/wordlift-admin-setup.js', array('jquery'));
29 29
 
30 30
 	//Get wp_permalink structure
31
-	$permalink_structure = get_option( 'permalink_structure' );
31
+	$permalink_structure = get_option('permalink_structure');
32 32
 	
33 33
 	// Set configuration settings.
34
-	wp_localize_script( 'wordlift-admin-setup', '_wlAdminSetup', array(
35
-		'ajaxUrl'   => parse_url( self_admin_url( 'admin-ajax.php' ), PHP_URL_PATH ),
34
+	wp_localize_script('wordlift-admin-setup', '_wlAdminSetup', array(
35
+		'ajaxUrl'   => parse_url(self_admin_url('admin-ajax.php'), PHP_URL_PATH),
36 36
 		'action'    => 'wl_validate_key',
37 37
 		'permalink' => $permalink_structure,
38 38
 		'media'     => array(
39
-			'title'  => __( 'WordLift Choose Logo', 'wordlift' ),
40
-			'button' => array( 'text' => __( 'Choose Logo', 'wordlift' ) ),
39
+			'title'  => __('WordLift Choose Logo', 'wordlift'),
40
+			'button' => array('text' => __('Choose Logo', 'wordlift')),
41 41
 		),
42
-	) );
42
+	));
43 43
 
44 44
 	// Finally print styles and scripts.
45 45
 	wp_print_styles();
@@ -51,56 +51,56 @@  discard block
 block discarded – undo
51 51
 
52 52
 	<!-- Pane 1 content -->
53 53
 	<script type="text/html" id="page-0">
54
-		<h2 class="page-title"><?php esc_html_e( 'Welcome', 'wordlift' ); ?></h2>
54
+		<h2 class="page-title"><?php esc_html_e('Welcome', 'wordlift'); ?></h2>
55 55
 		<p class="page-txt">
56
-			<?php esc_html_e( 'Thank you for downloading WordLift. Now you can boost your website with a double-digit growth. WordLift helps you with:', 'wordlift' ); ?>
56
+			<?php esc_html_e('Thank you for downloading WordLift. Now you can boost your website with a double-digit growth. WordLift helps you with:', 'wordlift'); ?>
57 57
 		</p>
58 58
 		<ul class="page-list">
59 59
 			<li>
60 60
 				<span class="fa fa-university"></span>
61
-				<?php esc_html_e( 'Trustworthiness', 'wordlift' ); ?>
61
+				<?php esc_html_e('Trustworthiness', 'wordlift'); ?>
62 62
 			</li>
63 63
 
64 64
 			<li>
65 65
 				<span class="fa fa-map-marker"></span>
66
-				<?php esc_html_e( 'Enrichment', 'wordlift' ); ?>
66
+				<?php esc_html_e('Enrichment', 'wordlift'); ?>
67 67
 			</li>
68 68
 
69 69
 			<li>
70 70
 				<span class="fa fa-heart"></span>
71
-				<?php esc_html_e( 'Engagement', 'wordlift' ); ?>
71
+				<?php esc_html_e('Engagement', 'wordlift'); ?>
72 72
 			</li>
73 73
 
74 74
 			<li>
75 75
 				<span class="fa fa-hand-o-right"></span>
76
-				<?php esc_html_e( 'Smart Navigation', 'wordlift' ); ?>
76
+				<?php esc_html_e('Smart Navigation', 'wordlift'); ?>
77 77
 			</li>
78 78
 
79 79
 			<li>
80 80
 				<span class="fa fa-google"></span>
81
-				<?php esc_html_e( 'SEO Optimization', 'wordlift' ); ?>
81
+				<?php esc_html_e('SEO Optimization', 'wordlift'); ?>
82 82
 			</li>
83 83
 
84 84
 			<li>
85 85
 				<span class="fa fa-group"></span>
86
-				<?php esc_html_e( 'Content Marketing', 'wordlift' ); ?>
86
+				<?php esc_html_e('Content Marketing', 'wordlift'); ?>
87 87
 			</li>
88 88
 		</ul>
89 89
 		<div class="btn-wrapper">
90 90
 			<a href="https://wordlift.io/blogger/?utm_campaign=wl_activation_learn_more"
91 91
 			   target="_tab"
92
-			   class="button"><?php esc_html_e( 'Learn More', 'wordlift' ); ?></a>
92
+			   class="button"><?php esc_html_e('Learn More', 'wordlift'); ?></a>
93 93
 			<input type="button" data-wl-next="wl-next"
94 94
 			       class="wl-default-action"
95
-			       value="<?php esc_attr_e( 'Next', 'wordlift' ); ?>">
95
+			       value="<?php esc_attr_e('Next', 'wordlift'); ?>">
96 96
 		</div>
97 97
 	</script>
98 98
 
99 99
 	<!-- Pane 2 content -->
100 100
 	<script type="text/html" id="page-1">
101
-		<h2 class="page-title"><?php esc_html_e( 'License Key', 'wordlift' ); ?></h2>
101
+		<h2 class="page-title"><?php esc_html_e('License Key', 'wordlift'); ?></h2>
102 102
 		<p class="page-txt">
103
-			<?php esc_html_e( 'If you already puchased a plan, check your email, get the activation key from your inbox and insert it in the field below. Otherwise ....', 'wordlift' ); ?>
103
+			<?php esc_html_e('If you already puchased a plan, check your email, get the activation key from your inbox and insert it in the field below. Otherwise ....', 'wordlift'); ?>
104 104
 		</p>
105 105
 		<input type="text" data-wl-key="wl-key" class="invalid untouched"
106 106
 		       id="key" name="key" value=""
@@ -109,39 +109,39 @@  discard block
 block discarded – undo
109 109
 			<a
110 110
 				href="https://wordlift.io/?utm_campaign=wl_activation_grab_the_key#plan-and-price"
111 111
 				target="_tab"
112
-				class="button"><?php esc_html_e( 'Grab a Key!', 'wordlift' ); ?></a><input
112
+				class="button"><?php esc_html_e('Grab a Key!', 'wordlift'); ?></a><input
113 113
 				type="button" data-wl-next="wl-next" class="wl-default-action"
114
-				value="<?php esc_attr_e( 'Next', 'wordlift' ); ?>">
114
+				value="<?php esc_attr_e('Next', 'wordlift'); ?>">
115 115
 		</div>
116 116
 	</script>
117 117
 
118 118
 	<!-- Pane 3 content -->
119 119
 	<script type="text/html" id="page-2">
120
-		<h2 class="page-title"><?php esc_html_e( 'Vocabulary', 'wordlift' ); ?></h2>
120
+		<h2 class="page-title"><?php esc_html_e('Vocabulary', 'wordlift'); ?></h2>
121 121
 		<p class="page-txt">
122
-			<?php esc_html_e( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field below. Check our FAQs if you need more info.', 'wordlift' ); ?>
122
+			<?php esc_html_e('All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field below. Check our FAQs if you need more info.', 'wordlift'); ?>
123 123
 		</p>
124 124
 		<input type="text" id="vocabulary" name="vocabulary" autocomplete="off"
125 125
 		       value="vocabulary"
126 126
 		       class="valid untouched" data-wl-vocabulary="wl-vocabulary">
127 127
 		<p class="page-det">
128
-			<?php esc_html_e( 'Leave it empty to place your entities in the root folder of your website', 'wordlift' ); ?>
128
+			<?php esc_html_e('Leave it empty to place your entities in the root folder of your website', 'wordlift'); ?>
129 129
 		</p>
130 130
 		<div class="btn-wrapper">
131 131
 			<input type="button" data-wl-next="wl-next"
132 132
 			       class="wl-default-action"
133
-			       value="<?php esc_attr_e( 'Next', 'wordlift' ); ?>">
133
+			       value="<?php esc_attr_e('Next', 'wordlift'); ?>">
134 134
 		</div>
135 135
 	</script>
136 136
 
137 137
 	<!-- Pane 4 content -->
138 138
 	<script type="text/html" id="page-3">
139
-		<h2 class="page-title"><?php esc_html_e( 'Language', 'wordlift' ); ?></h2>
139
+		<h2 class="page-title"><?php esc_html_e('Language', 'wordlift'); ?></h2>
140 140
 		<p class="page-txt">
141
-			<?php esc_html_e( 'Each WordLift key can be used only in one language. Pick yours.', 'wordlift' ); ?>
141
+			<?php esc_html_e('Each WordLift key can be used only in one language. Pick yours.', 'wordlift'); ?>
142 142
 		</p>
143 143
 		<select id="language" name="language"
144
-		        placeholder="<?php esc_attr_e( 'Choose your language', 'wordlift' ); ?>">
144
+		        placeholder="<?php esc_attr_e('Choose your language', 'wordlift'); ?>">
145 145
 			<?php
146 146
 
147 147
 			// Get WordLift's supported languages.
@@ -151,30 +151,30 @@  discard block
 block discarded – undo
151 151
 			$locale = get_locale();
152 152
 
153 153
 			// Get the language locale part.
154
-			$parts = explode( '_', $locale );
154
+			$parts = explode('_', $locale);
155 155
 
156 156
 			// If we support WP's configured language, then use that, otherwise use English by default.
157
-			$language = isset( $languages[ $parts[0] ] ) ? $parts[0] : 'en';
157
+			$language = isset($languages[$parts[0]]) ? $parts[0] : 'en';
158 158
 
159 159
 			// Print all the supported language, preselecting the one configured in WP (or English if not supported).
160
-			foreach ( $languages as $code => $label ) { ?>
160
+			foreach ($languages as $code => $label) { ?>
161 161
 				<option
162
-					value="<?php echo esc_attr( $code ) ?>" <?php echo selected( $code, $language, false ) ?>><?php echo esc_html( $label ) ?></option>
162
+					value="<?php echo esc_attr($code) ?>" <?php echo selected($code, $language, false) ?>><?php echo esc_html($label) ?></option>
163 163
 			<?php } ?>
164 164
 		</select>
165 165
 
166 166
 		<div class="btn-wrapper">
167 167
 			<input type="button" data-wl-next="wl-next"
168 168
 			       class="wl-default-action"
169
-			       value="<?php esc_attr_e( 'Next', 'wordlift' ); ?>">
169
+			       value="<?php esc_attr_e('Next', 'wordlift'); ?>">
170 170
 		</div>
171 171
 	</script>
172 172
 
173 173
 	<!-- Pane 5 content -->
174 174
 	<script type="text/html" id="page-4">
175
-		<h2 class="page-title"><?php esc_html_e( 'Publisher', 'wordlift' ); ?></h2>
175
+		<h2 class="page-title"><?php esc_html_e('Publisher', 'wordlift'); ?></h2>
176 176
 		<p class="page-txt">
177
-			<?php esc_html_e( 'Are you going to publish as an individual or as a company?', 'wordlift' ); ?>
177
+			<?php esc_html_e('Are you going to publish as an individual or as a company?', 'wordlift'); ?>
178 178
 		</p>
179 179
 		<div class="radio-wrapper">
180 180
 			<label for="personal">
@@ -182,20 +182,20 @@  discard block
 block discarded – undo
182 182
 				       value="person" checked>
183 183
 				<span class="radio"><span class="check"></span></span>
184 184
 				<span
185
-					class="label"><?php esc_html_e( 'Personal', 'wordlift' ); ?></span>
185
+					class="label"><?php esc_html_e('Personal', 'wordlift'); ?></span>
186 186
 			</label>
187 187
 			<label for="company">
188 188
 				<input id="company" type="radio" name="user_type"
189 189
 				       value="organization">
190 190
 				<span class="radio"><span class="check"></span></span>
191 191
 				<span
192
-					class="label"><?php esc_html_e( 'Company', 'wordlift' ); ?></span>
192
+					class="label"><?php esc_html_e('Company', 'wordlift'); ?></span>
193 193
 			</label>
194 194
 		</div>
195 195
 		<input type="text" id="name" name="name" data-wl-name="wl-name" value=""
196 196
 		       autocomplete="off"
197 197
 		       class="untouched invalid"
198
-		       placeholder="<?php esc_attr_e( "What's your name?", 'wordlift' ); ?>">
198
+		       placeholder="<?php esc_attr_e("What's your name?", 'wordlift'); ?>">
199 199
 
200 200
 		<div data-wl-logo="wl-logo">
201 201
 			<input type="hidden" name="logo" />
@@ -205,12 +205,12 @@  discard block
 block discarded – undo
205 205
 			</div>
206 206
 			<a data-wl-add-logo="wl-add-logo" class="add-logo"
207 207
 			   href="javascript:void(0);">
208
-				<?php esc_html_e( 'Add your logo', 'wordlift' ); ?>
208
+				<?php esc_html_e('Add your logo', 'wordlift'); ?>
209 209
 			</a>
210 210
 		</div>
211 211
 		<div class="btn-wrapper">
212 212
 			<input type="submit" id="btn-finish" class="wl-default-action"
213
-			       value="<?php esc_attr_e( 'Finish', 'wordlift' ); ?>">
213
+			       value="<?php esc_attr_e('Finish', 'wordlift'); ?>">
214 214
 		</div>
215 215
 	</script>
216 216
 
@@ -219,25 +219,25 @@  discard block
 block discarded – undo
219 219
 
220 220
 <div class="wl-container">
221 221
 
222
-	<a href="<?php echo esc_url( admin_url() ); ?> "
222
+	<a href="<?php echo esc_url(admin_url()); ?> "
223 223
 	   class="fa fa-times wl-close"></a>
224 224
 
225 225
 	<header>
226 226
 		<h1><strong>Word</strong>Lift</h1>
227 227
 		<img
228
-			src="<?php echo plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'images/shapes.png'; ?>" />
228
+			src="<?php echo plugin_dir_url(dirname(dirname(__FILE__))).'images/shapes.png'; ?>" />
229 229
 	</header>
230 230
 
231 231
 
232 232
 	<form method="post">
233
-		<?php wp_nonce_field( 'wl-save-configuration' ); ?>
233
+		<?php wp_nonce_field('wl-save-configuration'); ?>
234 234
 		<input type="hidden" name="action" value="wl-save-configuration" />
235 235
 		<div class="viewport"></div>
236 236
 	</form>
237 237
 
238 238
 </div>
239 239
 
240
-<?php do_action( 'admin_footer' ); ?>
240
+<?php do_action('admin_footer'); ?>
241 241
 
242 242
 </body>
243 243
 </html>
Please login to merge, or discard this patch.
src/includes/class-wordlift-user-service.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -346,7 +346,7 @@
 block discarded – undo
346 346
 	 *
347 347
 	 * @param int $user_id The {@link WP_User} `id`.
348 348
 	 *
349
-	 * @return int bool True if editing is denied otherwise false.
349
+	 * @return boolean bool True if editing is denied otherwise false.
350 350
 	 */
351 351
 	public function is_deny_editor_entity_create( $user_id ) {
352 352
 
Please login to merge, or discard this patch.
Indentation   +424 added lines, -424 removed lines patch added patch discarded remove patch
@@ -10,436 +10,436 @@
 block discarded – undo
10 10
  */
11 11
 class Wordlift_User_Service {
12 12
 
13
-	/**
14
-	 * The meta key where the user's URI is stored.
15
-	 *
16
-	 * @since 3.1.7
17
-	 */
18
-	const URI_META_KEY = '_wl_uri';
19
-
20
-	/**
21
-	 * The user meta key where the deny entity edit flag is stored.
22
-	 *
23
-	 * @since 3.14.0
24
-	 */
25
-	const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create';
26
-
27
-	/**
28
-	 * The meta key holding the entity id representing a {@link WP_User}.
29
-	 *
30
-	 * @since 3.14.0
31
-	 */
32
-	const ENTITY_META_KEY = '_wl_entity';
33
-
34
-	/**
35
-	 * The Log service.
36
-	 *
37
-	 * @since  3.1.7
38
-	 * @access private
39
-	 * @var \Wordlift_Log_Service $log_service The Log service.
40
-	 */
41
-	private $log_service;
42
-
43
-	/**
44
-	 * The singleton instance of the User service.
45
-	 *
46
-	 * @since  3.1.7
47
-	 * @access private
48
-	 * @var \Wordlift_User_Service $user_service The singleton instance of the User service.
49
-	 */
50
-	private static $instance;
51
-
52
-	/**
53
-	 * Create an instance of the User service.
54
-	 *
55
-	 * @since 3.1.7
56
-	 */
57
-	public function __construct() {
58
-
59
-		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' );
60
-
61
-		self::$instance = $this;
62
-
63
-		add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 );
64
-	}
65
-
66
-	/**
67
-	 * Get the singleton instance of the User service.
68
-	 *
69
-	 * @since 3.1.7
70
-	 * @return \Wordlift_User_Service The singleton instance of the User service.
71
-	 */
72
-	public static function get_instance() {
73
-
74
-		return self::$instance;
75
-	}
76
-
77
-	/**
78
-	 * Get the URI for a user.
79
-	 *
80
-	 * @since 3.1.7
81
-	 *
82
-	 * @param int $user_id The user id
83
-	 *
84
-	 * @return false|string The user's URI or false in case of failure.
85
-	 */
86
-	public function get_uri( $user_id ) {
87
-
88
-		// Try to get the URI stored in the user's meta and return it if available.
89
-		if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) {
90
-			return $user_uri;
91
-		}
92
-
93
-		// Try to build an URI, return false in case of failure.
94
-		if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) {
95
-			return false;
96
-		}
97
-
98
-		// Store the URI for future requests (we need a "permanent" URI).
99
-		$this->_set_uri( $user_id, $user_uri );
100
-
101
-		return $user_uri;
102
-	}
103
-
104
-	/**
105
-	 * Receives wp_insert_post events.
106
-	 *
107
-	 * @since 3.1.7
108
-	 *
109
-	 * @param int     $post_id Post ID.
110
-	 * @param WP_Post $post    Post object.
111
-	 * @param bool    $update  Whether this is an existing post being updated or not.
112
-	 */
113
-	public function wp_insert_post( $post_id, $post, $update ) {
114
-
115
-		// If the post is not published, return.
116
-		if ( 'publish' !== get_post_status( $post_id ) ) {
117
-			return;
118
-		}
119
-
120
-		// We expect a numeric author id.
121
-		if ( ! is_numeric( $post->post_author ) ) {
122
-			return;
123
-		}
124
-
125
-		// Get the delete query,or return in case of failure.
126
-		if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) {
127
-			return;
128
-		}
129
-
130
-		// Get the insert query,or return in case of failure.
131
-		if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) {
132
-			return;
133
-		}
134
-
135
-		// Send the query to the triple store.
136
-		rl_execute_sparql_update_query( $delete . $insert );
137
-
138
-	}
139
-
140
-	/**
141
-	 * Set the `id` of the entity representing a {@link WP_User}.
142
-	 *
143
-	 * If the `id` is set to 0 (or less) then the meta is deleted.
144
-	 *
145
-	 * @since 3.14.0
146
-	 *
147
-	 * @param int $user_id The {@link WP_User}.
148
-	 * @param int $value   The entity {@link WP_Post} `id`.
149
-	 *
150
-	 * @return bool|int  Meta ID if the key didn't exist, true on successful update, false on failure.
151
-	 */
152
-	public function set_entity( $user_id, $value ) {
153
-
154
-		return 0 < $value
155
-			? update_user_meta( $user_id, self::ENTITY_META_KEY, $value )
156
-			: delete_user_meta( $user_id, self::ENTITY_META_KEY );
157
-	}
158
-
159
-	/**
160
-	 * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}.
161
-	 *
162
-	 * @since 3.14.0
163
-	 *
164
-	 * @param int $user_id The {@link WP_User}'s `id`.
165
-	 *
166
-	 * @return string The entity {@link WP_Post} `id` or an empty string if not set.
167
-	 */
168
-	public function get_entity( $user_id ) {
169
-
170
-		return get_user_meta( $user_id, self::ENTITY_META_KEY, true );
171
-	}
172
-
173
-	/**
174
-	 * Get the user's URI stored in the user's meta.
175
-	 *
176
-	 * @since 3.1.7
177
-	 *
178
-	 * @param int $user_id The user id.
179
-	 *
180
-	 * @return false|string The user's URI or false if not found.
181
-	 */
182
-	private function _get_uri( $user_id ) {
183
-
184
-		$user_uri = get_user_meta( $user_id, self::URI_META_KEY, true );
185
-
186
-		if ( empty( $user_uri ) ) {
187
-			return false;
188
-		}
189
-
190
-		return $user_uri;
191
-	}
192
-
193
-	/**
194
-	 * Build an URI for a user.
195
-	 *
196
-	 * @since 3.1.7
197
-	 *
198
-	 * @param int $user_id The user's id.
199
-	 *
200
-	 * @return false|string The user's URI or false in case of failure.
201
-	 */
202
-	private function _build_uri( $user_id ) {
203
-
204
-		// Get the user, return false in case of failure.
205
-		if ( false === ( $user = get_userdata( $user_id ) ) ) {
206
-			return false;
207
-		};
208
-
209
-		// If the nicename is not set, return a failure.
210
-		if ( empty( $user->user_nicename ) ) {
211
-			return false;
212
-		}
213
-
214
-		return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename";
215
-	}
216
-
217
-	/**
218
-	 * Store the URI in user's meta.
219
-	 *
220
-	 * @since 3.1.7
221
-	 *
222
-	 * @param int    $user_id  The user's id.
223
-	 * @param string $user_uri The user's uri.
224
-	 *
225
-	 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
226
-	 */
227
-	private function _set_uri( $user_id, $user_uri ) {
228
-
229
-		return update_user_meta( $user_id, self::URI_META_KEY, $user_uri );
230
-	}
231
-
232
-	/**
233
-	 * Get the delete query.
234
-	 *
235
-	 * @since 3.1.7
236
-	 *
237
-	 * @param int $user_id The user id.
238
-	 *
239
-	 * @return false|string The delete query or false in case of failure.
240
-	 */
241
-	private function get_delete_query( $user_id ) {
242
-
243
-		// Get the URI, return if there's none.
244
-		if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
245
-			return false;
246
-		}
247
-
248
-		// Build the delete query.
249
-		$query = Wordlift_Query_Builder::new_instance()->delete()
250
-		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' )
251
-		                               ->build()
252
-		         . Wordlift_Query_Builder::new_instance()->delete()
253
-		                                 ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' )
254
-		                                 ->build()
255
-		         . Wordlift_Query_Builder::new_instance()->delete()
256
-		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' )
257
-		                                 ->build()
258
-		         . Wordlift_Query_Builder::new_instance()->delete()
259
-		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' )
260
-		                                 ->build()
261
-		         . Wordlift_Query_Builder::new_instance()->delete()
262
-		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' )
263
-		                                 ->build();
264
-
265
-		return $query;
266
-	}
267
-
268
-	/**
269
-	 * Get the insert query.
270
-	 *
271
-	 * @since 3.1.7
272
-	 *
273
-	 * @param int $user_id The user id.
274
-	 *
275
-	 * @return false|string The insert query or false in case of failure.
276
-	 */
277
-	private function get_insert_query( $user_id ) {
278
-
279
-		// Get the URI, return if there's none.
280
-		if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
281
-			return false;
282
-		}
283
-
284
-		// Try to get the user data, in case of failure return false.
285
-		if ( false === ( $user = get_userdata( $user_id ) ) ) {
286
-			return false;
287
-		};
288
-
289
-		// Build the insert query.
290
-		$query = Wordlift_Query_Builder::new_instance()
291
-		                               ->insert()
292
-		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI )
293
-		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name )
294
-		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname )
295
-		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname )
296
-		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) )
297
-		                               ->build();
298
-
299
-		return $query;
300
-	}
301
-
302
-	/**
303
-	 * Mark an editor user as denied from editing entities.
304
-	 * Does nothing if the user is not an editor
305
-	 *
306
-	 * @since 3.14.0
307
-	 *
308
-	 * @param integer $user_id The ID of the user
309
-	 */
310
-	public function deny_editor_entity_create( $user_id ) {
311
-
312
-		// Bail out if the user is not an editor.
313
-		if ( ! $this->is_editor( $user_id ) ) {
314
-			return;
315
-		}
316
-
317
-		// The user explicitly do not have the capability.
318
-		update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' );
319
-
320
-	}
321
-
322
-	/**
323
-	 * Remove the "deny entity editing" mark from an editor user.
324
-	 * Does nothing if the user is not an editor
325
-	 *
326
-	 * @since 3.14.0
327
-	 *
328
-	 * @param integer $user_id The ID of the user
329
-	 */
330
-	public function allow_editor_entity_create( $user_id ) {
331
-
332
-		// Bail out if the user is not an editor.
333
-		if ( ! $this->is_editor( $user_id ) ) {
334
-			return;
335
-		}
336
-
337
-		// The user explicitly do not have the capability.
338
-		delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY );
339
-
340
-	}
341
-
342
-	/**
343
-	 * Get whether the 'deny editor entity editing' flag is set.
344
-	 *
345
-	 * @since 3.14.0
346
-	 *
347
-	 * @param int $user_id The {@link WP_User} `id`.
348
-	 *
349
-	 * @return int bool True if editing is denied otherwise false.
350
-	 */
351
-	public function is_deny_editor_entity_create( $user_id ) {
352
-
353
-		return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id );
354
-	}
355
-
356
-	/**
357
-	 * Check whether the {@link WP_User} with the specified `id` is an editor,
358
-	 * i.e. has the `editor` role.
359
-	 *
360
-	 * @since 3.14.0
361
-	 *
362
-	 * @param int $user_id The {@link WP_User} `id`.
363
-	 *
364
-	 * @return bool True if the {@link WP_User} is an editor otherwise false.
365
-	 */
366
-	public function is_editor( $user_id ) {
367
-
368
-		// Get the user.
369
-		$user = get_user_by( 'id', $user_id );
370
-
371
-		// Return true, if the user is found and has the `editor` role.
372
-		return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles );
373
-	}
374
-
375
-	/**
376
-	 * Check if an editor can create entities.
377
-	 *
378
-	 * @since 3.14.0
379
-	 *
380
-	 * @param int $user_id The user id of the user being checked.
381
-	 *
382
-	 * @return bool    false if it is an editor that is denied from edit entities, true otherwise.
383
-	 */
384
-	public function editor_can_create_entities( $user_id ) {
385
-
386
-		// Return true if not an editor.
387
-		if ( ! $this->is_editor( $user_id ) ) {
388
-			return true;
389
-		}
390
-
391
-		// Check if the user explicitly denied.
392
-		return ! $this->is_deny_editor_entity_create( $user_id );
393
-	}
394
-
395
-	/**
396
-	 * Filter capabilities of user.
397
-	 *
398
-	 * Deny the capability of managing and editing entities for some users.
399
-	 *
400
-	 * @since 3.14.0
401
-	 *
402
-	 * @param array $allcaps All the capabilities of the user
403
-	 * @param array $cap     [0] Required capability
404
-	 * @param array $args    [0] Requested capability
405
-	 *                       [1] User ID
406
-	 *                       [2] Associated object ID
407
-	 *
408
-	 * @return array The capabilities array.
409
-	 */
410
-	public function has_cap( $allcaps, $cap, $args ) {
411
-		/*
13
+    /**
14
+     * The meta key where the user's URI is stored.
15
+     *
16
+     * @since 3.1.7
17
+     */
18
+    const URI_META_KEY = '_wl_uri';
19
+
20
+    /**
21
+     * The user meta key where the deny entity edit flag is stored.
22
+     *
23
+     * @since 3.14.0
24
+     */
25
+    const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create';
26
+
27
+    /**
28
+     * The meta key holding the entity id representing a {@link WP_User}.
29
+     *
30
+     * @since 3.14.0
31
+     */
32
+    const ENTITY_META_KEY = '_wl_entity';
33
+
34
+    /**
35
+     * The Log service.
36
+     *
37
+     * @since  3.1.7
38
+     * @access private
39
+     * @var \Wordlift_Log_Service $log_service The Log service.
40
+     */
41
+    private $log_service;
42
+
43
+    /**
44
+     * The singleton instance of the User service.
45
+     *
46
+     * @since  3.1.7
47
+     * @access private
48
+     * @var \Wordlift_User_Service $user_service The singleton instance of the User service.
49
+     */
50
+    private static $instance;
51
+
52
+    /**
53
+     * Create an instance of the User service.
54
+     *
55
+     * @since 3.1.7
56
+     */
57
+    public function __construct() {
58
+
59
+        $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' );
60
+
61
+        self::$instance = $this;
62
+
63
+        add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 );
64
+    }
65
+
66
+    /**
67
+     * Get the singleton instance of the User service.
68
+     *
69
+     * @since 3.1.7
70
+     * @return \Wordlift_User_Service The singleton instance of the User service.
71
+     */
72
+    public static function get_instance() {
73
+
74
+        return self::$instance;
75
+    }
76
+
77
+    /**
78
+     * Get the URI for a user.
79
+     *
80
+     * @since 3.1.7
81
+     *
82
+     * @param int $user_id The user id
83
+     *
84
+     * @return false|string The user's URI or false in case of failure.
85
+     */
86
+    public function get_uri( $user_id ) {
87
+
88
+        // Try to get the URI stored in the user's meta and return it if available.
89
+        if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) {
90
+            return $user_uri;
91
+        }
92
+
93
+        // Try to build an URI, return false in case of failure.
94
+        if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) {
95
+            return false;
96
+        }
97
+
98
+        // Store the URI for future requests (we need a "permanent" URI).
99
+        $this->_set_uri( $user_id, $user_uri );
100
+
101
+        return $user_uri;
102
+    }
103
+
104
+    /**
105
+     * Receives wp_insert_post events.
106
+     *
107
+     * @since 3.1.7
108
+     *
109
+     * @param int     $post_id Post ID.
110
+     * @param WP_Post $post    Post object.
111
+     * @param bool    $update  Whether this is an existing post being updated or not.
112
+     */
113
+    public function wp_insert_post( $post_id, $post, $update ) {
114
+
115
+        // If the post is not published, return.
116
+        if ( 'publish' !== get_post_status( $post_id ) ) {
117
+            return;
118
+        }
119
+
120
+        // We expect a numeric author id.
121
+        if ( ! is_numeric( $post->post_author ) ) {
122
+            return;
123
+        }
124
+
125
+        // Get the delete query,or return in case of failure.
126
+        if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) {
127
+            return;
128
+        }
129
+
130
+        // Get the insert query,or return in case of failure.
131
+        if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) {
132
+            return;
133
+        }
134
+
135
+        // Send the query to the triple store.
136
+        rl_execute_sparql_update_query( $delete . $insert );
137
+
138
+    }
139
+
140
+    /**
141
+     * Set the `id` of the entity representing a {@link WP_User}.
142
+     *
143
+     * If the `id` is set to 0 (or less) then the meta is deleted.
144
+     *
145
+     * @since 3.14.0
146
+     *
147
+     * @param int $user_id The {@link WP_User}.
148
+     * @param int $value   The entity {@link WP_Post} `id`.
149
+     *
150
+     * @return bool|int  Meta ID if the key didn't exist, true on successful update, false on failure.
151
+     */
152
+    public function set_entity( $user_id, $value ) {
153
+
154
+        return 0 < $value
155
+            ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value )
156
+            : delete_user_meta( $user_id, self::ENTITY_META_KEY );
157
+    }
158
+
159
+    /**
160
+     * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}.
161
+     *
162
+     * @since 3.14.0
163
+     *
164
+     * @param int $user_id The {@link WP_User}'s `id`.
165
+     *
166
+     * @return string The entity {@link WP_Post} `id` or an empty string if not set.
167
+     */
168
+    public function get_entity( $user_id ) {
169
+
170
+        return get_user_meta( $user_id, self::ENTITY_META_KEY, true );
171
+    }
172
+
173
+    /**
174
+     * Get the user's URI stored in the user's meta.
175
+     *
176
+     * @since 3.1.7
177
+     *
178
+     * @param int $user_id The user id.
179
+     *
180
+     * @return false|string The user's URI or false if not found.
181
+     */
182
+    private function _get_uri( $user_id ) {
183
+
184
+        $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true );
185
+
186
+        if ( empty( $user_uri ) ) {
187
+            return false;
188
+        }
189
+
190
+        return $user_uri;
191
+    }
192
+
193
+    /**
194
+     * Build an URI for a user.
195
+     *
196
+     * @since 3.1.7
197
+     *
198
+     * @param int $user_id The user's id.
199
+     *
200
+     * @return false|string The user's URI or false in case of failure.
201
+     */
202
+    private function _build_uri( $user_id ) {
203
+
204
+        // Get the user, return false in case of failure.
205
+        if ( false === ( $user = get_userdata( $user_id ) ) ) {
206
+            return false;
207
+        };
208
+
209
+        // If the nicename is not set, return a failure.
210
+        if ( empty( $user->user_nicename ) ) {
211
+            return false;
212
+        }
213
+
214
+        return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename";
215
+    }
216
+
217
+    /**
218
+     * Store the URI in user's meta.
219
+     *
220
+     * @since 3.1.7
221
+     *
222
+     * @param int    $user_id  The user's id.
223
+     * @param string $user_uri The user's uri.
224
+     *
225
+     * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
226
+     */
227
+    private function _set_uri( $user_id, $user_uri ) {
228
+
229
+        return update_user_meta( $user_id, self::URI_META_KEY, $user_uri );
230
+    }
231
+
232
+    /**
233
+     * Get the delete query.
234
+     *
235
+     * @since 3.1.7
236
+     *
237
+     * @param int $user_id The user id.
238
+     *
239
+     * @return false|string The delete query or false in case of failure.
240
+     */
241
+    private function get_delete_query( $user_id ) {
242
+
243
+        // Get the URI, return if there's none.
244
+        if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
245
+            return false;
246
+        }
247
+
248
+        // Build the delete query.
249
+        $query = Wordlift_Query_Builder::new_instance()->delete()
250
+                                        ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' )
251
+                                        ->build()
252
+                    . Wordlift_Query_Builder::new_instance()->delete()
253
+                                            ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' )
254
+                                            ->build()
255
+                    . Wordlift_Query_Builder::new_instance()->delete()
256
+                                            ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' )
257
+                                            ->build()
258
+                    . Wordlift_Query_Builder::new_instance()->delete()
259
+                                            ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' )
260
+                                            ->build()
261
+                    . Wordlift_Query_Builder::new_instance()->delete()
262
+                                            ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' )
263
+                                            ->build();
264
+
265
+        return $query;
266
+    }
267
+
268
+    /**
269
+     * Get the insert query.
270
+     *
271
+     * @since 3.1.7
272
+     *
273
+     * @param int $user_id The user id.
274
+     *
275
+     * @return false|string The insert query or false in case of failure.
276
+     */
277
+    private function get_insert_query( $user_id ) {
278
+
279
+        // Get the URI, return if there's none.
280
+        if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
281
+            return false;
282
+        }
283
+
284
+        // Try to get the user data, in case of failure return false.
285
+        if ( false === ( $user = get_userdata( $user_id ) ) ) {
286
+            return false;
287
+        };
288
+
289
+        // Build the insert query.
290
+        $query = Wordlift_Query_Builder::new_instance()
291
+                                        ->insert()
292
+                                        ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI )
293
+                                        ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name )
294
+                                        ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname )
295
+                                        ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname )
296
+                                        ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) )
297
+                                        ->build();
298
+
299
+        return $query;
300
+    }
301
+
302
+    /**
303
+     * Mark an editor user as denied from editing entities.
304
+     * Does nothing if the user is not an editor
305
+     *
306
+     * @since 3.14.0
307
+     *
308
+     * @param integer $user_id The ID of the user
309
+     */
310
+    public function deny_editor_entity_create( $user_id ) {
311
+
312
+        // Bail out if the user is not an editor.
313
+        if ( ! $this->is_editor( $user_id ) ) {
314
+            return;
315
+        }
316
+
317
+        // The user explicitly do not have the capability.
318
+        update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' );
319
+
320
+    }
321
+
322
+    /**
323
+     * Remove the "deny entity editing" mark from an editor user.
324
+     * Does nothing if the user is not an editor
325
+     *
326
+     * @since 3.14.0
327
+     *
328
+     * @param integer $user_id The ID of the user
329
+     */
330
+    public function allow_editor_entity_create( $user_id ) {
331
+
332
+        // Bail out if the user is not an editor.
333
+        if ( ! $this->is_editor( $user_id ) ) {
334
+            return;
335
+        }
336
+
337
+        // The user explicitly do not have the capability.
338
+        delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY );
339
+
340
+    }
341
+
342
+    /**
343
+     * Get whether the 'deny editor entity editing' flag is set.
344
+     *
345
+     * @since 3.14.0
346
+     *
347
+     * @param int $user_id The {@link WP_User} `id`.
348
+     *
349
+     * @return int bool True if editing is denied otherwise false.
350
+     */
351
+    public function is_deny_editor_entity_create( $user_id ) {
352
+
353
+        return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id );
354
+    }
355
+
356
+    /**
357
+     * Check whether the {@link WP_User} with the specified `id` is an editor,
358
+     * i.e. has the `editor` role.
359
+     *
360
+     * @since 3.14.0
361
+     *
362
+     * @param int $user_id The {@link WP_User} `id`.
363
+     *
364
+     * @return bool True if the {@link WP_User} is an editor otherwise false.
365
+     */
366
+    public function is_editor( $user_id ) {
367
+
368
+        // Get the user.
369
+        $user = get_user_by( 'id', $user_id );
370
+
371
+        // Return true, if the user is found and has the `editor` role.
372
+        return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles );
373
+    }
374
+
375
+    /**
376
+     * Check if an editor can create entities.
377
+     *
378
+     * @since 3.14.0
379
+     *
380
+     * @param int $user_id The user id of the user being checked.
381
+     *
382
+     * @return bool    false if it is an editor that is denied from edit entities, true otherwise.
383
+     */
384
+    public function editor_can_create_entities( $user_id ) {
385
+
386
+        // Return true if not an editor.
387
+        if ( ! $this->is_editor( $user_id ) ) {
388
+            return true;
389
+        }
390
+
391
+        // Check if the user explicitly denied.
392
+        return ! $this->is_deny_editor_entity_create( $user_id );
393
+    }
394
+
395
+    /**
396
+     * Filter capabilities of user.
397
+     *
398
+     * Deny the capability of managing and editing entities for some users.
399
+     *
400
+     * @since 3.14.0
401
+     *
402
+     * @param array $allcaps All the capabilities of the user
403
+     * @param array $cap     [0] Required capability
404
+     * @param array $args    [0] Requested capability
405
+     *                       [1] User ID
406
+     *                       [2] Associated object ID
407
+     *
408
+     * @return array The capabilities array.
409
+     */
410
+    public function has_cap( $allcaps, $cap, $args ) {
411
+        /*
412 412
 		 * For entity management/editing related capabilities
413 413
 		 * check that an editor was not explicitly denied (in user profile)
414 414
 		 * the capability.
415 415
 		 */
416 416
 
417
-		/*
417
+        /*
418 418
 		 * Need protection against the case of edit_user and likes which do not
419 419
 		 * require a capability, just request one.
420 420
 		 */
421
-		if ( empty( $cap ) ) {
422
-			return $allcaps;
423
-		}
424
-		if (
425
-			( 'edit_wordlift_entity' == $cap[0] ) ||
426
-			( 'edit_wordlift_entities' == $cap[0] ) ||
427
-			( 'edit_others_wordlift_entities' == $cap[0] ) ||
428
-			( 'publish_wordlift_entities' == $cap[0] ) ||
429
-			( 'read_private_wordlift_entities' == $cap[0] ) ||
430
-			( 'delete_wordlift_entity' == $cap[0] ) ||
431
-			( 'delete_wordlift_entities' == $cap[0] ) ||
432
-			( 'delete_others_wordlift_entities' == $cap[0] ) ||
433
-			( 'delete_published_wordlift_entities' == $cap[0] ) ||
434
-			( 'delete_private_wordlift_entities' == $cap[0] )
435
-		) {
436
-			$user_id = $args[1];
437
-
438
-			if ( ! $this->editor_can_create_entities( $user_id ) ) {
439
-				$allcaps[ $cap[0] ] = false;
440
-			}
441
-		}
442
-
443
-		return $allcaps;
444
-	}
421
+        if ( empty( $cap ) ) {
422
+            return $allcaps;
423
+        }
424
+        if (
425
+            ( 'edit_wordlift_entity' == $cap[0] ) ||
426
+            ( 'edit_wordlift_entities' == $cap[0] ) ||
427
+            ( 'edit_others_wordlift_entities' == $cap[0] ) ||
428
+            ( 'publish_wordlift_entities' == $cap[0] ) ||
429
+            ( 'read_private_wordlift_entities' == $cap[0] ) ||
430
+            ( 'delete_wordlift_entity' == $cap[0] ) ||
431
+            ( 'delete_wordlift_entities' == $cap[0] ) ||
432
+            ( 'delete_others_wordlift_entities' == $cap[0] ) ||
433
+            ( 'delete_published_wordlift_entities' == $cap[0] ) ||
434
+            ( 'delete_private_wordlift_entities' == $cap[0] )
435
+        ) {
436
+            $user_id = $args[1];
437
+
438
+            if ( ! $this->editor_can_create_entities( $user_id ) ) {
439
+                $allcaps[ $cap[0] ] = false;
440
+            }
441
+        }
442
+
443
+        return $allcaps;
444
+    }
445 445
 }
Please login to merge, or discard this patch.
Spacing   +69 added lines, -69 removed lines patch added patch discarded remove patch
@@ -56,11 +56,11 @@  discard block
 block discarded – undo
56 56
 	 */
57 57
 	public function __construct() {
58 58
 
59
-		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' );
59
+		$this->log_service = Wordlift_Log_Service::get_logger('Wordlift_User_Service');
60 60
 
61 61
 		self::$instance = $this;
62 62
 
63
-		add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 );
63
+		add_filter('user_has_cap', array($this, 'has_cap'), 10, 3);
64 64
 	}
65 65
 
66 66
 	/**
@@ -83,20 +83,20 @@  discard block
 block discarded – undo
83 83
 	 *
84 84
 	 * @return false|string The user's URI or false in case of failure.
85 85
 	 */
86
-	public function get_uri( $user_id ) {
86
+	public function get_uri($user_id) {
87 87
 
88 88
 		// Try to get the URI stored in the user's meta and return it if available.
89
-		if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) {
89
+		if (false !== ($user_uri = $this->_get_uri($user_id))) {
90 90
 			return $user_uri;
91 91
 		}
92 92
 
93 93
 		// Try to build an URI, return false in case of failure.
94
-		if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) {
94
+		if (false === ($user_uri = $this->_build_uri($user_id))) {
95 95
 			return false;
96 96
 		}
97 97
 
98 98
 		// Store the URI for future requests (we need a "permanent" URI).
99
-		$this->_set_uri( $user_id, $user_uri );
99
+		$this->_set_uri($user_id, $user_uri);
100 100
 
101 101
 		return $user_uri;
102 102
 	}
@@ -110,30 +110,30 @@  discard block
 block discarded – undo
110 110
 	 * @param WP_Post $post    Post object.
111 111
 	 * @param bool    $update  Whether this is an existing post being updated or not.
112 112
 	 */
113
-	public function wp_insert_post( $post_id, $post, $update ) {
113
+	public function wp_insert_post($post_id, $post, $update) {
114 114
 
115 115
 		// If the post is not published, return.
116
-		if ( 'publish' !== get_post_status( $post_id ) ) {
116
+		if ('publish' !== get_post_status($post_id)) {
117 117
 			return;
118 118
 		}
119 119
 
120 120
 		// We expect a numeric author id.
121
-		if ( ! is_numeric( $post->post_author ) ) {
121
+		if ( ! is_numeric($post->post_author)) {
122 122
 			return;
123 123
 		}
124 124
 
125 125
 		// Get the delete query,or return in case of failure.
126
-		if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) {
126
+		if (false === ($delete = $this->get_delete_query($post->post_author))) {
127 127
 			return;
128 128
 		}
129 129
 
130 130
 		// Get the insert query,or return in case of failure.
131
-		if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) {
131
+		if (false === ($insert = $this->get_insert_query($post->post_author))) {
132 132
 			return;
133 133
 		}
134 134
 
135 135
 		// Send the query to the triple store.
136
-		rl_execute_sparql_update_query( $delete . $insert );
136
+		rl_execute_sparql_update_query($delete.$insert);
137 137
 
138 138
 	}
139 139
 
@@ -149,11 +149,11 @@  discard block
 block discarded – undo
149 149
 	 *
150 150
 	 * @return bool|int  Meta ID if the key didn't exist, true on successful update, false on failure.
151 151
 	 */
152
-	public function set_entity( $user_id, $value ) {
152
+	public function set_entity($user_id, $value) {
153 153
 
154 154
 		return 0 < $value
155
-			? update_user_meta( $user_id, self::ENTITY_META_KEY, $value )
156
-			: delete_user_meta( $user_id, self::ENTITY_META_KEY );
155
+			? update_user_meta($user_id, self::ENTITY_META_KEY, $value)
156
+			: delete_user_meta($user_id, self::ENTITY_META_KEY);
157 157
 	}
158 158
 
159 159
 	/**
@@ -165,9 +165,9 @@  discard block
 block discarded – undo
165 165
 	 *
166 166
 	 * @return string The entity {@link WP_Post} `id` or an empty string if not set.
167 167
 	 */
168
-	public function get_entity( $user_id ) {
168
+	public function get_entity($user_id) {
169 169
 
170
-		return get_user_meta( $user_id, self::ENTITY_META_KEY, true );
170
+		return get_user_meta($user_id, self::ENTITY_META_KEY, true);
171 171
 	}
172 172
 
173 173
 	/**
@@ -179,11 +179,11 @@  discard block
 block discarded – undo
179 179
 	 *
180 180
 	 * @return false|string The user's URI or false if not found.
181 181
 	 */
182
-	private function _get_uri( $user_id ) {
182
+	private function _get_uri($user_id) {
183 183
 
184
-		$user_uri = get_user_meta( $user_id, self::URI_META_KEY, true );
184
+		$user_uri = get_user_meta($user_id, self::URI_META_KEY, true);
185 185
 
186
-		if ( empty( $user_uri ) ) {
186
+		if (empty($user_uri)) {
187 187
 			return false;
188 188
 		}
189 189
 
@@ -199,19 +199,19 @@  discard block
 block discarded – undo
199 199
 	 *
200 200
 	 * @return false|string The user's URI or false in case of failure.
201 201
 	 */
202
-	private function _build_uri( $user_id ) {
202
+	private function _build_uri($user_id) {
203 203
 
204 204
 		// Get the user, return false in case of failure.
205
-		if ( false === ( $user = get_userdata( $user_id ) ) ) {
205
+		if (false === ($user = get_userdata($user_id))) {
206 206
 			return false;
207 207
 		};
208 208
 
209 209
 		// If the nicename is not set, return a failure.
210
-		if ( empty( $user->user_nicename ) ) {
210
+		if (empty($user->user_nicename)) {
211 211
 			return false;
212 212
 		}
213 213
 
214
-		return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename";
214
+		return wl_configuration_get_redlink_dataset_uri()."/user/$user->user_nicename";
215 215
 	}
216 216
 
217 217
 	/**
@@ -224,9 +224,9 @@  discard block
 block discarded – undo
224 224
 	 *
225 225
 	 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
226 226
 	 */
227
-	private function _set_uri( $user_id, $user_uri ) {
227
+	private function _set_uri($user_id, $user_uri) {
228 228
 
229
-		return update_user_meta( $user_id, self::URI_META_KEY, $user_uri );
229
+		return update_user_meta($user_id, self::URI_META_KEY, $user_uri);
230 230
 	}
231 231
 
232 232
 	/**
@@ -238,28 +238,28 @@  discard block
 block discarded – undo
238 238
 	 *
239 239
 	 * @return false|string The delete query or false in case of failure.
240 240
 	 */
241
-	private function get_delete_query( $user_id ) {
241
+	private function get_delete_query($user_id) {
242 242
 
243 243
 		// Get the URI, return if there's none.
244
-		if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
244
+		if (false === ($user_uri = $this->get_uri($user_id))) {
245 245
 			return false;
246 246
 		}
247 247
 
248 248
 		// Build the delete query.
249 249
 		$query = Wordlift_Query_Builder::new_instance()->delete()
250
-		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' )
250
+		                               ->statement($user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o')
251 251
 		                               ->build()
252 252
 		         . Wordlift_Query_Builder::new_instance()->delete()
253
-		                                 ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' )
253
+		                                 ->statement($user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o')
254 254
 		                                 ->build()
255 255
 		         . Wordlift_Query_Builder::new_instance()->delete()
256
-		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' )
256
+		                                 ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o')
257 257
 		                                 ->build()
258 258
 		         . Wordlift_Query_Builder::new_instance()->delete()
259
-		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' )
259
+		                                 ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o')
260 260
 		                                 ->build()
261 261
 		         . Wordlift_Query_Builder::new_instance()->delete()
262
-		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' )
262
+		                                 ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o')
263 263
 		                                 ->build();
264 264
 
265 265
 		return $query;
@@ -274,26 +274,26 @@  discard block
 block discarded – undo
274 274
 	 *
275 275
 	 * @return false|string The insert query or false in case of failure.
276 276
 	 */
277
-	private function get_insert_query( $user_id ) {
277
+	private function get_insert_query($user_id) {
278 278
 
279 279
 		// Get the URI, return if there's none.
280
-		if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
280
+		if (false === ($user_uri = $this->get_uri($user_id))) {
281 281
 			return false;
282 282
 		}
283 283
 
284 284
 		// Try to get the user data, in case of failure return false.
285
-		if ( false === ( $user = get_userdata( $user_id ) ) ) {
285
+		if (false === ($user = get_userdata($user_id))) {
286 286
 			return false;
287 287
 		};
288 288
 
289 289
 		// Build the insert query.
290 290
 		$query = Wordlift_Query_Builder::new_instance()
291 291
 		                               ->insert()
292
-		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI )
293
-		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name )
294
-		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname )
295
-		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname )
296
-		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) )
292
+		                               ->statement($user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI)
293
+		                               ->statement($user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name)
294
+		                               ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname)
295
+		                               ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname)
296
+		                               ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty($user->user_url) ? $user->user_url : get_author_posts_url($user_id)))
297 297
 		                               ->build();
298 298
 
299 299
 		return $query;
@@ -307,15 +307,15 @@  discard block
 block discarded – undo
307 307
 	 *
308 308
 	 * @param integer $user_id The ID of the user
309 309
 	 */
310
-	public function deny_editor_entity_create( $user_id ) {
310
+	public function deny_editor_entity_create($user_id) {
311 311
 
312 312
 		// Bail out if the user is not an editor.
313
-		if ( ! $this->is_editor( $user_id ) ) {
313
+		if ( ! $this->is_editor($user_id)) {
314 314
 			return;
315 315
 		}
316 316
 
317 317
 		// The user explicitly do not have the capability.
318
-		update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' );
318
+		update_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes');
319 319
 
320 320
 	}
321 321
 
@@ -327,15 +327,15 @@  discard block
 block discarded – undo
327 327
 	 *
328 328
 	 * @param integer $user_id The ID of the user
329 329
 	 */
330
-	public function allow_editor_entity_create( $user_id ) {
330
+	public function allow_editor_entity_create($user_id) {
331 331
 
332 332
 		// Bail out if the user is not an editor.
333
-		if ( ! $this->is_editor( $user_id ) ) {
333
+		if ( ! $this->is_editor($user_id)) {
334 334
 			return;
335 335
 		}
336 336
 
337 337
 		// The user explicitly do not have the capability.
338
-		delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY );
338
+		delete_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY);
339 339
 
340 340
 	}
341 341
 
@@ -348,9 +348,9 @@  discard block
 block discarded – undo
348 348
 	 *
349 349
 	 * @return int bool True if editing is denied otherwise false.
350 350
 	 */
351
-	public function is_deny_editor_entity_create( $user_id ) {
351
+	public function is_deny_editor_entity_create($user_id) {
352 352
 
353
-		return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id );
353
+		return 'yes' === get_user_option(self::DENY_ENTITY_CREATE_META_KEY, $user_id);
354 354
 	}
355 355
 
356 356
 	/**
@@ -363,13 +363,13 @@  discard block
 block discarded – undo
363 363
 	 *
364 364
 	 * @return bool True if the {@link WP_User} is an editor otherwise false.
365 365
 	 */
366
-	public function is_editor( $user_id ) {
366
+	public function is_editor($user_id) {
367 367
 
368 368
 		// Get the user.
369
-		$user = get_user_by( 'id', $user_id );
369
+		$user = get_user_by('id', $user_id);
370 370
 
371 371
 		// Return true, if the user is found and has the `editor` role.
372
-		return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles );
372
+		return is_a($user, 'WP_User') && in_array('editor', (array) $user->roles);
373 373
 	}
374 374
 
375 375
 	/**
@@ -381,15 +381,15 @@  discard block
 block discarded – undo
381 381
 	 *
382 382
 	 * @return bool    false if it is an editor that is denied from edit entities, true otherwise.
383 383
 	 */
384
-	public function editor_can_create_entities( $user_id ) {
384
+	public function editor_can_create_entities($user_id) {
385 385
 
386 386
 		// Return true if not an editor.
387
-		if ( ! $this->is_editor( $user_id ) ) {
387
+		if ( ! $this->is_editor($user_id)) {
388 388
 			return true;
389 389
 		}
390 390
 
391 391
 		// Check if the user explicitly denied.
392
-		return ! $this->is_deny_editor_entity_create( $user_id );
392
+		return ! $this->is_deny_editor_entity_create($user_id);
393 393
 	}
394 394
 
395 395
 	/**
@@ -407,7 +407,7 @@  discard block
 block discarded – undo
407 407
 	 *
408 408
 	 * @return array The capabilities array.
409 409
 	 */
410
-	public function has_cap( $allcaps, $cap, $args ) {
410
+	public function has_cap($allcaps, $cap, $args) {
411 411
 		/*
412 412
 		 * For entity management/editing related capabilities
413 413
 		 * check that an editor was not explicitly denied (in user profile)
@@ -418,25 +418,25 @@  discard block
 block discarded – undo
418 418
 		 * Need protection against the case of edit_user and likes which do not
419 419
 		 * require a capability, just request one.
420 420
 		 */
421
-		if ( empty( $cap ) ) {
421
+		if (empty($cap)) {
422 422
 			return $allcaps;
423 423
 		}
424 424
 		if (
425
-			( 'edit_wordlift_entity' == $cap[0] ) ||
426
-			( 'edit_wordlift_entities' == $cap[0] ) ||
427
-			( 'edit_others_wordlift_entities' == $cap[0] ) ||
428
-			( 'publish_wordlift_entities' == $cap[0] ) ||
429
-			( 'read_private_wordlift_entities' == $cap[0] ) ||
430
-			( 'delete_wordlift_entity' == $cap[0] ) ||
431
-			( 'delete_wordlift_entities' == $cap[0] ) ||
432
-			( 'delete_others_wordlift_entities' == $cap[0] ) ||
433
-			( 'delete_published_wordlift_entities' == $cap[0] ) ||
434
-			( 'delete_private_wordlift_entities' == $cap[0] )
425
+			('edit_wordlift_entity' == $cap[0]) ||
426
+			('edit_wordlift_entities' == $cap[0]) ||
427
+			('edit_others_wordlift_entities' == $cap[0]) ||
428
+			('publish_wordlift_entities' == $cap[0]) ||
429
+			('read_private_wordlift_entities' == $cap[0]) ||
430
+			('delete_wordlift_entity' == $cap[0]) ||
431
+			('delete_wordlift_entities' == $cap[0]) ||
432
+			('delete_others_wordlift_entities' == $cap[0]) ||
433
+			('delete_published_wordlift_entities' == $cap[0]) ||
434
+			('delete_private_wordlift_entities' == $cap[0])
435 435
 		) {
436 436
 			$user_id = $args[1];
437 437
 
438
-			if ( ! $this->editor_can_create_entities( $user_id ) ) {
439
-				$allcaps[ $cap[0] ] = false;
438
+			if ( ! $this->editor_can_create_entities($user_id)) {
439
+				$allcaps[$cap[0]] = false;
440 440
 			}
441 441
 		}
442 442
 
Please login to merge, or discard this patch.
src/modules/core/wordlift_core_constants.php 1 patch
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -1,54 +1,54 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-define( 'WL_DEFAULT_THUMBNAIL_PATH', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/public/images/missing-image-150x150.png' );
4
-define( 'WL_DEFAULT_PATH', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/' );
3
+define('WL_DEFAULT_THUMBNAIL_PATH', dirname(dirname(plugin_dir_url(__FILE__))).'/public/images/missing-image-150x150.png');
4
+define('WL_DEFAULT_PATH', dirname(dirname(plugin_dir_url(__FILE__))).'/');
5 5
 
6 6
 // Database version
7
-define( 'WL_DB_VERSION', '3.14' );
7
+define('WL_DB_VERSION', '3.14');
8 8
 // Custom table name
9
-define( 'WL_DB_RELATION_INSTANCES_TABLE_NAME', 'wl_relation_instances' );
9
+define('WL_DB_RELATION_INSTANCES_TABLE_NAME', 'wl_relation_instances');
10 10
 
11
-define( 'WL_WHAT_RELATION', 'what' );
12
-define( 'WL_WHO_RELATION', 'who' );
13
-define( 'WL_WHERE_RELATION', 'where' );
14
-define( 'WL_WHEN_RELATION', 'when' );
11
+define('WL_WHAT_RELATION', 'what');
12
+define('WL_WHO_RELATION', 'who');
13
+define('WL_WHERE_RELATION', 'where');
14
+define('WL_WHEN_RELATION', 'when');
15 15
 
16 16
 // Mapping options / validations rules used by wl_core_get_posts to perform validation on args
17 17
 // The array is serialized because array constants are only from php 5.6 on.
18
-define( 'WL_CORE_GET_POSTS_VALIDATION_RULES', serialize( array(
19
-	'get'            => array( 'posts', 'post_ids' ),
20
-	'as'             => array( 'object', 'subject' ),
21
-	'post_type'      => array( 'post', 'entity' ),
22
-	'post_status'    => array( 'draft', 'trash', 'publish' ),
23
-	'with_predicate' => array( WL_WHAT_RELATION, WL_WHEN_RELATION, WL_WHERE_RELATION, WL_WHO_RELATION ),
24
-) ) );
18
+define('WL_CORE_GET_POSTS_VALIDATION_RULES', serialize(array(
19
+	'get'            => array('posts', 'post_ids'),
20
+	'as'             => array('object', 'subject'),
21
+	'post_type'      => array('post', 'entity'),
22
+	'post_status'    => array('draft', 'trash', 'publish'),
23
+	'with_predicate' => array(WL_WHAT_RELATION, WL_WHEN_RELATION, WL_WHERE_RELATION, WL_WHO_RELATION),
24
+)));
25 25
 
26 26
 // Classification boxes configuration for angularjs edit-post widget
27 27
 // The array is serialized because array constants are only from php 5.6 on.
28 28
 
29
-define( 'WL_CORE_POST_CLASSIFICATION_BOXES', serialize( array(
29
+define('WL_CORE_POST_CLASSIFICATION_BOXES', serialize(array(
30 30
 	array(
31 31
 		'id'               => WL_WHAT_RELATION,
32 32
 		'label'            => 'What',
33
-		'registeredTypes'  => array( 'thing', 'creative-work' ),
33
+		'registeredTypes'  => array('thing', 'creative-work'),
34 34
 		'selectedEntities' => array(),
35 35
 	),
36 36
 	array(
37 37
 		'id'               => WL_WHO_RELATION,
38 38
 		'label'            => 'Who',
39
-		'registeredTypes'  => array( 'organization', 'person', 'local-business' ),
39
+		'registeredTypes'  => array('organization', 'person', 'local-business'),
40 40
 		'selectedEntities' => array(),
41 41
 	),
42 42
 	array(
43 43
 		'id'               => WL_WHERE_RELATION,
44 44
 		'label'            => 'Where',
45
-		'registeredTypes'  => array( 'place' ),
45
+		'registeredTypes'  => array('place'),
46 46
 		'selectedEntities' => array(),
47 47
 	),
48 48
 	array(
49 49
 		'id'               => WL_WHEN_RELATION,
50 50
 		'label'            => 'When',
51
-		'registeredTypes'  => array( 'event' ),
51
+		'registeredTypes'  => array('event'),
52 52
 		'selectedEntities' => array(),
53 53
 	),
54
-) ) );
54
+)));
Please login to merge, or discard this patch.
src/admin/class-wordlift-admin-user-profile-page.php 2 patches
Indentation   +119 added lines, -119 removed lines patch added patch discarded remove patch
@@ -18,75 +18,75 @@  discard block
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Admin_User_Profile_Page {
20 20
 
21
-	/**
22
-	 * The {@link Wordlift_Admin_Person_Element} instance.
23
-	 *
24
-	 * @since  3.14.0
25
-	 * @access private
26
-	 * @var \Wordlift_Admin_Author_Element $plugin The person entity
27
-	 *                selection element rendering the possible persons.
28
-	 */
29
-	private $author_element;
30
-
31
-	/**
32
-	 * The {@link Wordlift_User_Service} instance.
33
-	 *
34
-	 * @since  3.14.0
35
-	 * @access private
36
-	 * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance.
37
-	 */
38
-	private $user_service;
39
-
40
-	/**
41
-	 * Create the {@link Wordlift_Admin_User_Profile_Page} instance.
42
-	 *
43
-	 * @since 3.14.0
44
-	 *
45
-	 * @param \Wordlift_Admin_Author_Element $author_element The person entity selection
46
-	 *                                                       element rendering the possible persons.
47
-	 * @param \Wordlift_User_Service         $user_service   The {@link Wordlift_User_Service} instance.
48
-	 */
49
-	function __construct( $author_element, $user_service ) {
50
-
51
-		$this->author_element = $author_element;
52
-		$this->user_service   = $user_service;
53
-
54
-		/*
21
+    /**
22
+     * The {@link Wordlift_Admin_Person_Element} instance.
23
+     *
24
+     * @since  3.14.0
25
+     * @access private
26
+     * @var \Wordlift_Admin_Author_Element $plugin The person entity
27
+     *                selection element rendering the possible persons.
28
+     */
29
+    private $author_element;
30
+
31
+    /**
32
+     * The {@link Wordlift_User_Service} instance.
33
+     *
34
+     * @since  3.14.0
35
+     * @access private
36
+     * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance.
37
+     */
38
+    private $user_service;
39
+
40
+    /**
41
+     * Create the {@link Wordlift_Admin_User_Profile_Page} instance.
42
+     *
43
+     * @since 3.14.0
44
+     *
45
+     * @param \Wordlift_Admin_Author_Element $author_element The person entity selection
46
+     *                                                       element rendering the possible persons.
47
+     * @param \Wordlift_User_Service         $user_service   The {@link Wordlift_User_Service} instance.
48
+     */
49
+    function __construct( $author_element, $user_service ) {
50
+
51
+        $this->author_element = $author_element;
52
+        $this->user_service   = $user_service;
53
+
54
+        /*
55 55
 		 * When an admin (or similar permissions) edits his own profile a
56 56
 		 * different action than the usual is being triggered.
57 57
 		 * It is too early in the wordpress boot to do user capabilities filtering
58 58
 		 * here and it is deferred to the handler.
59 59
 		 */
60
-		add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );
61
-		add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
62
-		add_action( 'edit_user_profile_update', array(
63
-			$this,
64
-			'edit_user_profile_update',
65
-		) );
66
-		add_action( 'personal_options_update', array(
67
-			$this,
68
-			'edit_user_profile_update',
69
-		) );
70
-
71
-	}
72
-
73
-	/**
74
-	 * Add a WordLift section in the user profile which lets
75
-	 * the admin to associate a wordpress user with a person entity.
76
-	 *
77
-	 * @since 3.14.0
78
-	 *
79
-	 * @param WP_User $user The current WP_User object of the user being edited.
80
-	 */
81
-	public function edit_user_profile( $user ) {
82
-
83
-		// In case it is a user editing his own profile, make sure he has admin
84
-		// like capabilities.
85
-		if ( ! current_user_can( 'edit_users' ) ) {
86
-			return;
87
-		}
88
-
89
-		?>
60
+        add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );
61
+        add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
62
+        add_action( 'edit_user_profile_update', array(
63
+            $this,
64
+            'edit_user_profile_update',
65
+        ) );
66
+        add_action( 'personal_options_update', array(
67
+            $this,
68
+            'edit_user_profile_update',
69
+        ) );
70
+
71
+    }
72
+
73
+    /**
74
+     * Add a WordLift section in the user profile which lets
75
+     * the admin to associate a wordpress user with a person entity.
76
+     *
77
+     * @since 3.14.0
78
+     *
79
+     * @param WP_User $user The current WP_User object of the user being edited.
80
+     */
81
+    public function edit_user_profile( $user ) {
82
+
83
+        // In case it is a user editing his own profile, make sure he has admin
84
+        // like capabilities.
85
+        if ( ! current_user_can( 'edit_users' ) ) {
86
+            return;
87
+        }
88
+
89
+        ?>
90 90
 		<h2><?php esc_html_e( 'WordLift', 'wordlift' ); ?></h2>
91 91
 
92 92
 		<table class="form-table">
@@ -96,12 +96,12 @@  discard block
 block discarded – undo
96 96
 				</th>
97 97
 				<td>
98 98
 					<?php
99
-					$this->author_element->render( array(
100
-						'id'             => 'wl_person',
101
-						'name'           => 'wl_person',
102
-						'current_entity' => $this->user_service->get_entity( $user->ID ),
103
-					) );
104
-					?>
99
+                    $this->author_element->render( array(
100
+                        'id'             => 'wl_person',
101
+                        'name'           => 'wl_person',
102
+                        'current_entity' => $this->user_service->get_entity( $user->ID ),
103
+                    ) );
104
+                    ?>
105 105
 					<p class="description"><?php _e( 'The entity, person or organization, from the vocabulary to associate with this author.', 'wordlift' ); ?></p>
106 106
 				</td>
107 107
 			</tr>
@@ -119,54 +119,54 @@  discard block
 block discarded – undo
119 119
 				<?php } ?>
120 120
 		</table>
121 121
 		<?php
122
-	}
123
-
124
-	/**
125
-	 * Handle storing the person entity associated with the user.
126
-	 *
127
-	 * @since 3.14.0
128
-	 *
129
-	 * @param int $user_id The user id of the user being saved.
130
-	 */
131
-	public function edit_user_profile_update( $user_id ) {
132
-
133
-		// In case it is a user editing his own profile, make sure he has admin
134
-		// like capabilities.
135
-		if ( ! current_user_can( 'edit_users' ) ) {
136
-			return;
137
-		}
138
-
139
-		// Link an entity to the user.
140
-		$this->link_entity( $user_id, $_POST );
141
-
142
-		// Deny and enable the edit entity capability
143
-		if ( isset( $_POST['wl_can_create_entities'] ) ) {
144
-			// User has capability so remove the deny indication if present.
145
-			$this->user_service->allow_editor_entity_create( $user_id );
146
-		} else {
147
-			$this->user_service->deny_editor_entity_create( $user_id );
148
-		}
149
-
150
-	}
151
-
152
-	/**
153
-	 * Link an entity (specified in the `$_POST` array) to the {@link WP_User}
154
-	 * with the specified `id`.
155
-	 *
156
-	 * @since 3.14.0
157
-	 *
158
-	 * @param int   $user_id The {@link WP_User} `id`.
159
-	 * @param array $post    The `$_POST` array.
160
-	 */
161
-	private function link_entity( $user_id, $post ) {
162
-
163
-		// Bail out if the `wl_person` parameter isn't set.
164
-		if ( ! isset( $post['wl_person'] ) || ! is_numeric( $post['wl_person'] ) ) {
165
-			return;
166
-		}
167
-
168
-		$this->user_service->set_entity( $user_id, intval( $post['wl_person'] ) );
169
-
170
-	}
122
+    }
123
+
124
+    /**
125
+     * Handle storing the person entity associated with the user.
126
+     *
127
+     * @since 3.14.0
128
+     *
129
+     * @param int $user_id The user id of the user being saved.
130
+     */
131
+    public function edit_user_profile_update( $user_id ) {
132
+
133
+        // In case it is a user editing his own profile, make sure he has admin
134
+        // like capabilities.
135
+        if ( ! current_user_can( 'edit_users' ) ) {
136
+            return;
137
+        }
138
+
139
+        // Link an entity to the user.
140
+        $this->link_entity( $user_id, $_POST );
141
+
142
+        // Deny and enable the edit entity capability
143
+        if ( isset( $_POST['wl_can_create_entities'] ) ) {
144
+            // User has capability so remove the deny indication if present.
145
+            $this->user_service->allow_editor_entity_create( $user_id );
146
+        } else {
147
+            $this->user_service->deny_editor_entity_create( $user_id );
148
+        }
149
+
150
+    }
151
+
152
+    /**
153
+     * Link an entity (specified in the `$_POST` array) to the {@link WP_User}
154
+     * with the specified `id`.
155
+     *
156
+     * @since 3.14.0
157
+     *
158
+     * @param int   $user_id The {@link WP_User} `id`.
159
+     * @param array $post    The `$_POST` array.
160
+     */
161
+    private function link_entity( $user_id, $post ) {
162
+
163
+        // Bail out if the `wl_person` parameter isn't set.
164
+        if ( ! isset( $post['wl_person'] ) || ! is_numeric( $post['wl_person'] ) ) {
165
+            return;
166
+        }
167
+
168
+        $this->user_service->set_entity( $user_id, intval( $post['wl_person'] ) );
169
+
170
+    }
171 171
 
172 172
 }
Please login to merge, or discard this patch.
Spacing   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
 	 *                                                       element rendering the possible persons.
47 47
 	 * @param \Wordlift_User_Service         $user_service   The {@link Wordlift_User_Service} instance.
48 48
 	 */
49
-	function __construct( $author_element, $user_service ) {
49
+	function __construct($author_element, $user_service) {
50 50
 
51 51
 		$this->author_element = $author_element;
52 52
 		$this->user_service   = $user_service;
@@ -57,16 +57,16 @@  discard block
 block discarded – undo
57 57
 		 * It is too early in the wordpress boot to do user capabilities filtering
58 58
 		 * here and it is deferred to the handler.
59 59
 		 */
60
-		add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );
61
-		add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
62
-		add_action( 'edit_user_profile_update', array(
60
+		add_action('show_user_profile', array($this, 'edit_user_profile'));
61
+		add_action('edit_user_profile', array($this, 'edit_user_profile'));
62
+		add_action('edit_user_profile_update', array(
63 63
 			$this,
64 64
 			'edit_user_profile_update',
65
-		) );
66
-		add_action( 'personal_options_update', array(
65
+		));
66
+		add_action('personal_options_update', array(
67 67
 			$this,
68 68
 			'edit_user_profile_update',
69
-		) );
69
+		));
70 70
 
71 71
 	}
72 72
 
@@ -78,43 +78,43 @@  discard block
 block discarded – undo
78 78
 	 *
79 79
 	 * @param WP_User $user The current WP_User object of the user being edited.
80 80
 	 */
81
-	public function edit_user_profile( $user ) {
81
+	public function edit_user_profile($user) {
82 82
 
83 83
 		// In case it is a user editing his own profile, make sure he has admin
84 84
 		// like capabilities.
85
-		if ( ! current_user_can( 'edit_users' ) ) {
85
+		if ( ! current_user_can('edit_users')) {
86 86
 			return;
87 87
 		}
88 88
 
89 89
 		?>
90
-		<h2><?php esc_html_e( 'WordLift', 'wordlift' ); ?></h2>
90
+		<h2><?php esc_html_e('WordLift', 'wordlift'); ?></h2>
91 91
 
92 92
 		<table class="form-table">
93 93
 			<tr class="user-description-wrap">
94 94
 				<th><label
95
-						for="wl_person"><?php _e( 'Author from the vocabulary', 'wordlift' ); ?></label>
95
+						for="wl_person"><?php _e('Author from the vocabulary', 'wordlift'); ?></label>
96 96
 				</th>
97 97
 				<td>
98 98
 					<?php
99
-					$this->author_element->render( array(
99
+					$this->author_element->render(array(
100 100
 						'id'             => 'wl_person',
101 101
 						'name'           => 'wl_person',
102
-						'current_entity' => $this->user_service->get_entity( $user->ID ),
103
-					) );
102
+						'current_entity' => $this->user_service->get_entity($user->ID),
103
+					));
104 104
 					?>
105
-					<p class="description"><?php _e( 'The entity, person or organization, from the vocabulary to associate with this author.', 'wordlift' ); ?></p>
105
+					<p class="description"><?php _e('The entity, person or organization, from the vocabulary to associate with this author.', 'wordlift'); ?></p>
106 106
 				</td>
107 107
 			</tr>
108
-			<?php if ( $this->user_service->is_editor( $user->ID ) ) { ?>
108
+			<?php if ($this->user_service->is_editor($user->ID)) { ?>
109 109
 			<tr>
110 110
 				<th>
111 111
 					<label
112
-						for="wl_can_create_entities"><?php esc_html_e( 'Can create new entities', 'wordlift' ) ?></label>
112
+						for="wl_can_create_entities"><?php esc_html_e('Can create new entities', 'wordlift') ?></label>
113 113
 				</th>
114 114
 				<td>
115 115
 					<input id="wl_can_create_entities"
116 116
 					       name="wl_can_create_entities"
117
-					       type="checkbox" <?php checked( $this->user_service->editor_can_create_entities( $user->ID ) ) ?>
117
+					       type="checkbox" <?php checked($this->user_service->editor_can_create_entities($user->ID)) ?>
118 118
 				</td>
119 119
 				<?php } ?>
120 120
 		</table>
@@ -128,23 +128,23 @@  discard block
 block discarded – undo
128 128
 	 *
129 129
 	 * @param int $user_id The user id of the user being saved.
130 130
 	 */
131
-	public function edit_user_profile_update( $user_id ) {
131
+	public function edit_user_profile_update($user_id) {
132 132
 
133 133
 		// In case it is a user editing his own profile, make sure he has admin
134 134
 		// like capabilities.
135
-		if ( ! current_user_can( 'edit_users' ) ) {
135
+		if ( ! current_user_can('edit_users')) {
136 136
 			return;
137 137
 		}
138 138
 
139 139
 		// Link an entity to the user.
140
-		$this->link_entity( $user_id, $_POST );
140
+		$this->link_entity($user_id, $_POST);
141 141
 
142 142
 		// Deny and enable the edit entity capability
143
-		if ( isset( $_POST['wl_can_create_entities'] ) ) {
143
+		if (isset($_POST['wl_can_create_entities'])) {
144 144
 			// User has capability so remove the deny indication if present.
145
-			$this->user_service->allow_editor_entity_create( $user_id );
145
+			$this->user_service->allow_editor_entity_create($user_id);
146 146
 		} else {
147
-			$this->user_service->deny_editor_entity_create( $user_id );
147
+			$this->user_service->deny_editor_entity_create($user_id);
148 148
 		}
149 149
 
150 150
 	}
@@ -158,14 +158,14 @@  discard block
 block discarded – undo
158 158
 	 * @param int   $user_id The {@link WP_User} `id`.
159 159
 	 * @param array $post    The `$_POST` array.
160 160
 	 */
161
-	private function link_entity( $user_id, $post ) {
161
+	private function link_entity($user_id, $post) {
162 162
 
163 163
 		// Bail out if the `wl_person` parameter isn't set.
164
-		if ( ! isset( $post['wl_person'] ) || ! is_numeric( $post['wl_person'] ) ) {
164
+		if ( ! isset($post['wl_person']) || ! is_numeric($post['wl_person'])) {
165 165
 			return;
166 166
 		}
167 167
 
168
-		$this->user_service->set_entity( $user_id, intval( $post['wl_person'] ) );
168
+		$this->user_service->set_entity($user_id, intval($post['wl_person']));
169 169
 
170 170
 	}
171 171
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-service.php 1 patch
Indentation   +496 added lines, -496 removed lines patch added patch discarded remove patch
@@ -7,506 +7,506 @@
 block discarded – undo
7 7
  */
8 8
 class Wordlift_Entity_Service {
9 9
 
10
-	/**
11
-	 * The Log service.
12
-	 *
13
-	 * @since  3.2.0
14
-	 * @access private
15
-	 * @var \Wordlift_Log_Service $log The Log service.
16
-	 */
17
-	private $log;
18
-
19
-	/**
20
-	 * The UI service.
21
-	 *
22
-	 * @since  3.2.0
23
-	 * @access private
24
-	 * @var \Wordlift_UI_Service $ui_service The UI service.
25
-	 */
26
-	private $ui_service;
27
-
28
-	/**
29
-	 * The entity post type name.
30
-	 *
31
-	 * @since 3.1.0
32
-	 */
33
-	const TYPE_NAME = 'entity';
34
-
35
-	/**
36
-	 * The alternative label meta key.
37
-	 *
38
-	 * @since 3.2.0
39
-	 */
40
-	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
41
-
42
-	/**
43
-	 * The alternative label input template.
44
-	 *
45
-	 * @since 3.2.0
46
-	 */
47
-	// TODO: this should be moved to a class that deals with HTML code.
48
-	const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
10
+    /**
11
+     * The Log service.
12
+     *
13
+     * @since  3.2.0
14
+     * @access private
15
+     * @var \Wordlift_Log_Service $log The Log service.
16
+     */
17
+    private $log;
18
+
19
+    /**
20
+     * The UI service.
21
+     *
22
+     * @since  3.2.0
23
+     * @access private
24
+     * @var \Wordlift_UI_Service $ui_service The UI service.
25
+     */
26
+    private $ui_service;
27
+
28
+    /**
29
+     * The entity post type name.
30
+     *
31
+     * @since 3.1.0
32
+     */
33
+    const TYPE_NAME = 'entity';
34
+
35
+    /**
36
+     * The alternative label meta key.
37
+     *
38
+     * @since 3.2.0
39
+     */
40
+    const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
41
+
42
+    /**
43
+     * The alternative label input template.
44
+     *
45
+     * @since 3.2.0
46
+     */
47
+    // TODO: this should be moved to a class that deals with HTML code.
48
+    const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
49 49
                 <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label>
50 50
                 <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text">
51 51
                 <button class="button wl-delete-button">%s</button>
52 52
                 </div>';
53 53
 
54
-	/**
55
-	 * A singleton instance of the Entity service.
56
-	 *
57
-	 * @since  3.2.0
58
-	 * @access private
59
-	 * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
60
-	 */
61
-	private static $instance;
62
-
63
-	/**
64
-	 * Create a Wordlift_Entity_Service instance.
65
-	 *
66
-	 * @since 3.2.0
67
-	 *
68
-	 * @param \Wordlift_UI_Service $ui_service The UI service.
69
-	 */
70
-	public function __construct( $ui_service ) {
71
-
72
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
73
-
74
-		// Set the UI service.
75
-		$this->ui_service = $ui_service;
76
-
77
-		// Set the singleton instance.
78
-		self::$instance = $this;
79
-
80
-	}
81
-
82
-	/**
83
-	 * Get the singleton instance of the Entity service.
84
-	 *
85
-	 * @since 3.2.0
86
-	 * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
87
-	 */
88
-	public static function get_instance() {
89
-
90
-		return self::$instance;
91
-	}
92
-
93
-	/**
94
-	 * Determines whether a post is an entity or not.
95
-	 *
96
-	 * @since 3.1.0
97
-	 *
98
-	 * @param int $post_id A post id.
99
-	 *
100
-	 * @return bool Return true if the post is an entity otherwise false.
101
-	 */
102
-	public function is_entity( $post_id ) {
103
-
104
-		return ( self::TYPE_NAME === get_post_type( $post_id ) );
105
-	}
106
-
107
-	/**
108
-	 * Get the proper classification scope for a given entity post
109
-	 *
110
-	 * @since 3.5.0
111
-	 *
112
-	 * @param integer $post_id An entity post id.
113
-	 *
114
-	 * @return string Returns an uri.
115
-	 */
116
-	public function get_classification_scope_for( $post_id ) {
117
-
118
-		if ( false === $this->is_entity( $post_id ) ) {
119
-			return null;
120
-		}
121
-		// Retrieve the entity type
122
-		$entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id );
123
-		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
124
-		// Retrieve classification boxes configuration
125
-		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
126
-		foreach ( $classification_boxes as $cb ) {
127
-			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
128
-				return $cb['id'];
129
-			}
130
-		}
131
-
132
-		// or null
133
-		return null;
134
-
135
-	}
136
-
137
-
138
-	public function is_used( $post_id ) {
139
-
140
-		if ( false === $this->is_entity( $post_id ) ) {
141
-			return null;
142
-		}
143
-		// Retrieve the post
144
-		$entity = get_post( $post_id );
145
-
146
-		global $wpdb;
147
-		// Retrieve Wordlift relation instances table name
148
-		$table_name = wl_core_get_relation_instances_table_name();
149
-
150
-		// Check is it's referenced / related to another post / entity
151
-		$stmt = $wpdb->prepare(
152
-			"SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
153
-			$entity->ID
154
-		);
155
-
156
-		// Perform the query
157
-		$relation_instances = (int) $wpdb->get_var( $stmt );
158
-		// If there is at least one relation instance for the current entity, then it's used
159
-		if ( 0 < $relation_instances ) {
160
-			return true;
161
-		}
162
-
163
-		// Check if the entity uri is used as meta_value
164
-		$stmt = $wpdb->prepare(
165
-			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
166
-			$entity->ID,
167
-			wl_get_entity_uri( $entity->ID )
168
-		);
169
-		// Perform the query
170
-		$meta_instances = (int) $wpdb->get_var( $stmt );
171
-
172
-		// If there is at least one meta that refers the current entity uri, then current entity is used
173
-		if ( 0 < $meta_instances ) {
174
-			return true;
175
-		}
176
-
177
-		// If we are here, it means the current entity is not used at the moment
178
-		return false;
179
-	}
180
-
181
-	/**
182
-	 * Determines whether a given uri is an internal uri or not.
183
-	 *
184
-	 * @since 3.3.2
185
-	 *
186
-	 * @param int $uri An uri.
187
-	 *
188
-	 * @return true if the uri internal to the current dataset otherwise false.
189
-	 */
190
-	public function is_internal_uri( $uri ) {
191
-
192
-		return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) );
193
-	}
194
-
195
-	/**
196
-	 * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
197
-	 *
198
-	 * @since 3.2.0
199
-	 *
200
-	 * @param string $uri The entity URI.
201
-	 *
202
-	 * @return WP_Post|null A WP_Post instance or null if not found.
203
-	 */
204
-	public function get_entity_post_by_uri( $uri ) {
205
-
206
-		// Check if we've been provided with a value otherwise return null.
207
-		if ( empty( $uri ) ) {
208
-			return null;
209
-		}
210
-
211
-		$query_args = array(
212
-			'posts_per_page' => 1,
213
-			'post_status'    => 'any',
214
-			'post_type'      => self::TYPE_NAME,
215
-			'meta_query'     => array(
216
-				array(
217
-					'key'     => WL_ENTITY_URL_META_NAME,
218
-					'value'   => $uri,
219
-					'compare' => '=',
220
-				),
221
-			),
222
-		);
223
-
224
-		// Only if the current uri is not an internal uri, entity search is
225
-		// performed also looking at sameAs values.
226
-		//
227
-		// This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237
228
-		if ( ! $this->is_internal_uri( $uri ) ) {
229
-
230
-			$query_args['meta_query']['relation'] = 'OR';
231
-			$query_args['meta_query'][]           = array(
232
-				'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
233
-				'value'   => $uri,
234
-				'compare' => '=',
235
-			);
236
-		}
237
-
238
-		$query = new WP_Query( $query_args );
239
-
240
-		// Get the matching entity posts.
241
-		$posts = $query->get_posts();
242
-
243
-		// Return null if no post is found.
244
-		if ( 0 === count( $posts ) ) {
245
-			return null;
246
-		}
247
-
248
-		// Return the found post.
249
-		return $posts[0];
250
-	}
251
-
252
-	/**
253
-	 * Fires once a post has been saved. This function uses the $_REQUEST, therefore
254
-	 * we check that the post we're saving is the current post.
255
-	 *
256
-	 * @see   https://github.com/insideout10/wordlift-plugin/issues/363
257
-	 *
258
-	 * @since 3.2.0
259
-	 *
260
-	 * @param int     $post_id Post ID.
261
-	 * @param WP_Post $post    Post object.
262
-	 * @param bool    $update  Whether this is an existing post being updated or not.
263
-	 */
264
-	public function save_post( $post_id, $post, $update ) {
265
-
266
-		// Avoid doing anything if post is autosave or a revision.
267
-		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
268
-			return;
269
-		}
270
-
271
-		// We're setting the alternative label that have been provided via the UI
272
-		// (in fact we're using $_REQUEST), while save_post may be also called
273
-		// programmatically by some other function: we need to check therefore if
274
-		// the $post_id in the save_post call matches the post id set in the request.
275
-		//
276
-		// If this is not the current post being saved or if it's not an entity, return.
277
-		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
278
-			return;
279
-		}
280
-
281
-		// Get the alt labels from the request (or empty array).
282
-		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
283
-
284
-		// Set the alternative labels.
285
-		$this->set_alternative_labels( $post_id, $alt_labels );
286
-
287
-	}
288
-
289
-	/**
290
-	 * Set the alternative labels.
291
-	 *
292
-	 * @since 3.2.0
293
-	 *
294
-	 * @param int   $post_id    The post id.
295
-	 * @param array $alt_labels An array of labels.
296
-	 */
297
-	public function set_alternative_labels( $post_id, $alt_labels ) {
298
-
299
-		// Force $alt_labels to be an array
300
-		if ( ! is_array( $alt_labels ) ) {
301
-			$alt_labels = array( $alt_labels );
302
-		}
303
-
304
-		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
305
-
306
-		// Delete all the existing alternate labels.
307
-		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
308
-
309
-		// Set the alternative labels.
310
-		foreach ( $alt_labels as $alt_label ) {
311
-			if ( ! empty( $alt_label ) ) {
312
-				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
313
-			}
314
-		}
315
-
316
-	}
317
-
318
-	/**
319
-	 * Retrieve the alternate labels.
320
-	 *
321
-	 * @since 3.2.0
322
-	 *
323
-	 * @param int $post_id Post id.
324
-	 *
325
-	 * @return mixed An array  of alternative labels.
326
-	 */
327
-	public function get_alternative_labels( $post_id ) {
328
-
329
-		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
330
-	}
331
-
332
-	/**
333
-	 * Retrieve the labels for an entity, i.e. the title + the synonyms.
334
-	 *
335
-	 * @since 3.12.0
336
-	 *
337
-	 * @param int $post_id The entity {@link WP_Post} id.
338
-	 *
339
-	 * @return array An array with the entity title and labels.
340
-	 */
341
-	public function get_labels( $post_id ) {
342
-
343
-		return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
344
-	}
345
-
346
-	/**
347
-	 * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
348
-	 *
349
-	 * @since 3.2.0
350
-	 *
351
-	 * @param WP_Post $post Post object.
352
-	 */
353
-	public function edit_form_before_permalink( $post ) {
354
-
355
-		// If it's not an entity, return.
356
-		if ( ! $this->is_entity( $post->ID ) ) {
357
-			return;
358
-		}
359
-
360
-		// Print the input template.
361
-		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
362
-
363
-		// Print all the currently set alternative labels.
364
-		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
365
-
366
-			echo $this->get_alternative_label_input( $alt_label );
367
-
368
-		};
369
-
370
-		// Print the button.
371
-		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
372
-
373
-	}
374
-
375
-	/**
376
-	 * Get the URI for the entity with the specified post id.
377
-	 *
378
-	 * @since 3.6.0
379
-	 *
380
-	 * @param int $post_id The entity post id.
381
-	 *
382
-	 * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
383
-	 */
384
-	public function get_uri( $post_id ) {
385
-
386
-		// If a null is given, nothing to do
387
-		if ( null == $post_id ) {
388
-			return null;
389
-		}
390
-
391
-		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
392
-
393
-		// If the dataset uri is not properly configured, null is returned
394
-		if ( '' === wl_configuration_get_redlink_dataset_uri() ) {
395
-			return null;
396
-		}
397
-
398
-		// Set the URI if it isn't set yet.
399
-		$post_status = get_post_status( $post_id );
400
-		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
401
-			$uri = wl_build_entity_uri( $post_id );
402
-			wl_set_entity_uri( $post_id, $uri );
403
-		}
404
-
405
-		return $uri;
406
-	}
407
-
408
-
409
-	/**
410
-	 * Get the alternative label input HTML code.
411
-	 *
412
-	 * @since 3.2.0
413
-	 *
414
-	 * @param string $value The input value.
415
-	 *
416
-	 * @return string The input HTML code.
417
-	 */
418
-	private function get_alternative_label_input( $value = '' ) {
419
-
420
-		return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
421
-	}
422
-
423
-	/**
424
-	 * Get the number of entity posts published in this blog.
425
-	 *
426
-	 * @since 3.6.0
427
-	 *
428
-	 * @return int The number of published entity posts.
429
-	 */
430
-	public function count() {
431
-
432
-		$count = wp_count_posts( self::TYPE_NAME );
433
-
434
-		return $count->publish;
435
-	}
436
-
437
-	/**
438
-	 * Create a new entity.
439
-	 *
440
-	 * @since 3.9.0
441
-	 *
442
-	 * @param string $name     The entity name.
443
-	 * @param string $type_uri The entity's type URI.
444
-	 * @param null   $logo     The entity logo id (or NULL if none).
445
-	 * @param string $status   The post status, by default 'publish'.
446
-	 *
447
-	 * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
448
-	 */
449
-	public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
450
-
451
-		// Create an entity for the publisher.
452
-		$post_id = wp_insert_post( array(
453
-			'post_type'    => self::TYPE_NAME,
454
-			'post_title'   => $name,
455
-			'post_status'  => $status,
456
-			'post_content' => '',
457
-		) );
458
-
459
-		// Return the error if any.
460
-		if ( is_wp_error( $post_id ) ) {
461
-			return $post_id;
462
-		}
463
-
464
-		// Set the entity logo.
465
-		if ( $logo && is_numeric( $logo ) ) {
466
-			set_post_thumbnail( $post_id, $logo );
467
-		}
468
-
469
-		// Set the entity type.
470
-		Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
471
-
472
-		return $post_id;
473
-	}
474
-
475
-	/**
476
-	 * Get the entities related to the one with the specified id. By default only
477
-	 * published entities will be returned.
478
-	 *
479
-	 * @since 3.10.0
480
-	 *
481
-	 * @param int    $id          The post id.
482
-	 * @param string $post_status The target post status (default = publish).
483
-	 *
484
-	 * @return array An array of post ids.
485
-	 */
486
-	public function get_related_entities( $id, $post_status = 'publish' ) {
487
-
488
-		return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status );
489
-	}
490
-
491
-	/**
492
-	 * Get the list of entities.
493
-	 *
494
-	 * @since 3.12.2
495
-	 *
496
-	 * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
497
-	 *
498
-	 * @return array An array of entity posts.
499
-	 */
500
-	public function get( $params = array() ) {
501
-
502
-		// Set the defaults.
503
-		$defaults = array( 'post_type' => 'entity' );
504
-
505
-		// Merge the defaults with the provided parameters.
506
-		$args = wp_parse_args( $params, $defaults );
507
-
508
-		// Call the `get_posts` function.
509
-		return get_posts( $args );
510
-	}
54
+    /**
55
+     * A singleton instance of the Entity service.
56
+     *
57
+     * @since  3.2.0
58
+     * @access private
59
+     * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
60
+     */
61
+    private static $instance;
62
+
63
+    /**
64
+     * Create a Wordlift_Entity_Service instance.
65
+     *
66
+     * @since 3.2.0
67
+     *
68
+     * @param \Wordlift_UI_Service $ui_service The UI service.
69
+     */
70
+    public function __construct( $ui_service ) {
71
+
72
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
73
+
74
+        // Set the UI service.
75
+        $this->ui_service = $ui_service;
76
+
77
+        // Set the singleton instance.
78
+        self::$instance = $this;
79
+
80
+    }
81
+
82
+    /**
83
+     * Get the singleton instance of the Entity service.
84
+     *
85
+     * @since 3.2.0
86
+     * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
87
+     */
88
+    public static function get_instance() {
89
+
90
+        return self::$instance;
91
+    }
92
+
93
+    /**
94
+     * Determines whether a post is an entity or not.
95
+     *
96
+     * @since 3.1.0
97
+     *
98
+     * @param int $post_id A post id.
99
+     *
100
+     * @return bool Return true if the post is an entity otherwise false.
101
+     */
102
+    public function is_entity( $post_id ) {
103
+
104
+        return ( self::TYPE_NAME === get_post_type( $post_id ) );
105
+    }
106
+
107
+    /**
108
+     * Get the proper classification scope for a given entity post
109
+     *
110
+     * @since 3.5.0
111
+     *
112
+     * @param integer $post_id An entity post id.
113
+     *
114
+     * @return string Returns an uri.
115
+     */
116
+    public function get_classification_scope_for( $post_id ) {
117
+
118
+        if ( false === $this->is_entity( $post_id ) ) {
119
+            return null;
120
+        }
121
+        // Retrieve the entity type
122
+        $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id );
123
+        $entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
124
+        // Retrieve classification boxes configuration
125
+        $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
126
+        foreach ( $classification_boxes as $cb ) {
127
+            if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
128
+                return $cb['id'];
129
+            }
130
+        }
131
+
132
+        // or null
133
+        return null;
134
+
135
+    }
136
+
137
+
138
+    public function is_used( $post_id ) {
139
+
140
+        if ( false === $this->is_entity( $post_id ) ) {
141
+            return null;
142
+        }
143
+        // Retrieve the post
144
+        $entity = get_post( $post_id );
145
+
146
+        global $wpdb;
147
+        // Retrieve Wordlift relation instances table name
148
+        $table_name = wl_core_get_relation_instances_table_name();
149
+
150
+        // Check is it's referenced / related to another post / entity
151
+        $stmt = $wpdb->prepare(
152
+            "SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
153
+            $entity->ID
154
+        );
155
+
156
+        // Perform the query
157
+        $relation_instances = (int) $wpdb->get_var( $stmt );
158
+        // If there is at least one relation instance for the current entity, then it's used
159
+        if ( 0 < $relation_instances ) {
160
+            return true;
161
+        }
162
+
163
+        // Check if the entity uri is used as meta_value
164
+        $stmt = $wpdb->prepare(
165
+            "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
166
+            $entity->ID,
167
+            wl_get_entity_uri( $entity->ID )
168
+        );
169
+        // Perform the query
170
+        $meta_instances = (int) $wpdb->get_var( $stmt );
171
+
172
+        // If there is at least one meta that refers the current entity uri, then current entity is used
173
+        if ( 0 < $meta_instances ) {
174
+            return true;
175
+        }
176
+
177
+        // If we are here, it means the current entity is not used at the moment
178
+        return false;
179
+    }
180
+
181
+    /**
182
+     * Determines whether a given uri is an internal uri or not.
183
+     *
184
+     * @since 3.3.2
185
+     *
186
+     * @param int $uri An uri.
187
+     *
188
+     * @return true if the uri internal to the current dataset otherwise false.
189
+     */
190
+    public function is_internal_uri( $uri ) {
191
+
192
+        return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) );
193
+    }
194
+
195
+    /**
196
+     * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
197
+     *
198
+     * @since 3.2.0
199
+     *
200
+     * @param string $uri The entity URI.
201
+     *
202
+     * @return WP_Post|null A WP_Post instance or null if not found.
203
+     */
204
+    public function get_entity_post_by_uri( $uri ) {
205
+
206
+        // Check if we've been provided with a value otherwise return null.
207
+        if ( empty( $uri ) ) {
208
+            return null;
209
+        }
210
+
211
+        $query_args = array(
212
+            'posts_per_page' => 1,
213
+            'post_status'    => 'any',
214
+            'post_type'      => self::TYPE_NAME,
215
+            'meta_query'     => array(
216
+                array(
217
+                    'key'     => WL_ENTITY_URL_META_NAME,
218
+                    'value'   => $uri,
219
+                    'compare' => '=',
220
+                ),
221
+            ),
222
+        );
223
+
224
+        // Only if the current uri is not an internal uri, entity search is
225
+        // performed also looking at sameAs values.
226
+        //
227
+        // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237
228
+        if ( ! $this->is_internal_uri( $uri ) ) {
229
+
230
+            $query_args['meta_query']['relation'] = 'OR';
231
+            $query_args['meta_query'][]           = array(
232
+                'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
233
+                'value'   => $uri,
234
+                'compare' => '=',
235
+            );
236
+        }
237
+
238
+        $query = new WP_Query( $query_args );
239
+
240
+        // Get the matching entity posts.
241
+        $posts = $query->get_posts();
242
+
243
+        // Return null if no post is found.
244
+        if ( 0 === count( $posts ) ) {
245
+            return null;
246
+        }
247
+
248
+        // Return the found post.
249
+        return $posts[0];
250
+    }
251
+
252
+    /**
253
+     * Fires once a post has been saved. This function uses the $_REQUEST, therefore
254
+     * we check that the post we're saving is the current post.
255
+     *
256
+     * @see   https://github.com/insideout10/wordlift-plugin/issues/363
257
+     *
258
+     * @since 3.2.0
259
+     *
260
+     * @param int     $post_id Post ID.
261
+     * @param WP_Post $post    Post object.
262
+     * @param bool    $update  Whether this is an existing post being updated or not.
263
+     */
264
+    public function save_post( $post_id, $post, $update ) {
265
+
266
+        // Avoid doing anything if post is autosave or a revision.
267
+        if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
268
+            return;
269
+        }
270
+
271
+        // We're setting the alternative label that have been provided via the UI
272
+        // (in fact we're using $_REQUEST), while save_post may be also called
273
+        // programmatically by some other function: we need to check therefore if
274
+        // the $post_id in the save_post call matches the post id set in the request.
275
+        //
276
+        // If this is not the current post being saved or if it's not an entity, return.
277
+        if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
278
+            return;
279
+        }
280
+
281
+        // Get the alt labels from the request (or empty array).
282
+        $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
283
+
284
+        // Set the alternative labels.
285
+        $this->set_alternative_labels( $post_id, $alt_labels );
286
+
287
+    }
288
+
289
+    /**
290
+     * Set the alternative labels.
291
+     *
292
+     * @since 3.2.0
293
+     *
294
+     * @param int   $post_id    The post id.
295
+     * @param array $alt_labels An array of labels.
296
+     */
297
+    public function set_alternative_labels( $post_id, $alt_labels ) {
298
+
299
+        // Force $alt_labels to be an array
300
+        if ( ! is_array( $alt_labels ) ) {
301
+            $alt_labels = array( $alt_labels );
302
+        }
303
+
304
+        $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
305
+
306
+        // Delete all the existing alternate labels.
307
+        delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
308
+
309
+        // Set the alternative labels.
310
+        foreach ( $alt_labels as $alt_label ) {
311
+            if ( ! empty( $alt_label ) ) {
312
+                add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
313
+            }
314
+        }
315
+
316
+    }
317
+
318
+    /**
319
+     * Retrieve the alternate labels.
320
+     *
321
+     * @since 3.2.0
322
+     *
323
+     * @param int $post_id Post id.
324
+     *
325
+     * @return mixed An array  of alternative labels.
326
+     */
327
+    public function get_alternative_labels( $post_id ) {
328
+
329
+        return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
330
+    }
331
+
332
+    /**
333
+     * Retrieve the labels for an entity, i.e. the title + the synonyms.
334
+     *
335
+     * @since 3.12.0
336
+     *
337
+     * @param int $post_id The entity {@link WP_Post} id.
338
+     *
339
+     * @return array An array with the entity title and labels.
340
+     */
341
+    public function get_labels( $post_id ) {
342
+
343
+        return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
344
+    }
345
+
346
+    /**
347
+     * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
348
+     *
349
+     * @since 3.2.0
350
+     *
351
+     * @param WP_Post $post Post object.
352
+     */
353
+    public function edit_form_before_permalink( $post ) {
354
+
355
+        // If it's not an entity, return.
356
+        if ( ! $this->is_entity( $post->ID ) ) {
357
+            return;
358
+        }
359
+
360
+        // Print the input template.
361
+        $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
362
+
363
+        // Print all the currently set alternative labels.
364
+        foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
365
+
366
+            echo $this->get_alternative_label_input( $alt_label );
367
+
368
+        };
369
+
370
+        // Print the button.
371
+        $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
372
+
373
+    }
374
+
375
+    /**
376
+     * Get the URI for the entity with the specified post id.
377
+     *
378
+     * @since 3.6.0
379
+     *
380
+     * @param int $post_id The entity post id.
381
+     *
382
+     * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
383
+     */
384
+    public function get_uri( $post_id ) {
385
+
386
+        // If a null is given, nothing to do
387
+        if ( null == $post_id ) {
388
+            return null;
389
+        }
390
+
391
+        $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
392
+
393
+        // If the dataset uri is not properly configured, null is returned
394
+        if ( '' === wl_configuration_get_redlink_dataset_uri() ) {
395
+            return null;
396
+        }
397
+
398
+        // Set the URI if it isn't set yet.
399
+        $post_status = get_post_status( $post_id );
400
+        if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
401
+            $uri = wl_build_entity_uri( $post_id );
402
+            wl_set_entity_uri( $post_id, $uri );
403
+        }
404
+
405
+        return $uri;
406
+    }
407
+
408
+
409
+    /**
410
+     * Get the alternative label input HTML code.
411
+     *
412
+     * @since 3.2.0
413
+     *
414
+     * @param string $value The input value.
415
+     *
416
+     * @return string The input HTML code.
417
+     */
418
+    private function get_alternative_label_input( $value = '' ) {
419
+
420
+        return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
421
+    }
422
+
423
+    /**
424
+     * Get the number of entity posts published in this blog.
425
+     *
426
+     * @since 3.6.0
427
+     *
428
+     * @return int The number of published entity posts.
429
+     */
430
+    public function count() {
431
+
432
+        $count = wp_count_posts( self::TYPE_NAME );
433
+
434
+        return $count->publish;
435
+    }
436
+
437
+    /**
438
+     * Create a new entity.
439
+     *
440
+     * @since 3.9.0
441
+     *
442
+     * @param string $name     The entity name.
443
+     * @param string $type_uri The entity's type URI.
444
+     * @param null   $logo     The entity logo id (or NULL if none).
445
+     * @param string $status   The post status, by default 'publish'.
446
+     *
447
+     * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
448
+     */
449
+    public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
450
+
451
+        // Create an entity for the publisher.
452
+        $post_id = wp_insert_post( array(
453
+            'post_type'    => self::TYPE_NAME,
454
+            'post_title'   => $name,
455
+            'post_status'  => $status,
456
+            'post_content' => '',
457
+        ) );
458
+
459
+        // Return the error if any.
460
+        if ( is_wp_error( $post_id ) ) {
461
+            return $post_id;
462
+        }
463
+
464
+        // Set the entity logo.
465
+        if ( $logo && is_numeric( $logo ) ) {
466
+            set_post_thumbnail( $post_id, $logo );
467
+        }
468
+
469
+        // Set the entity type.
470
+        Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
471
+
472
+        return $post_id;
473
+    }
474
+
475
+    /**
476
+     * Get the entities related to the one with the specified id. By default only
477
+     * published entities will be returned.
478
+     *
479
+     * @since 3.10.0
480
+     *
481
+     * @param int    $id          The post id.
482
+     * @param string $post_status The target post status (default = publish).
483
+     *
484
+     * @return array An array of post ids.
485
+     */
486
+    public function get_related_entities( $id, $post_status = 'publish' ) {
487
+
488
+        return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status );
489
+    }
490
+
491
+    /**
492
+     * Get the list of entities.
493
+     *
494
+     * @since 3.12.2
495
+     *
496
+     * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
497
+     *
498
+     * @return array An array of entity posts.
499
+     */
500
+    public function get( $params = array() ) {
501
+
502
+        // Set the defaults.
503
+        $defaults = array( 'post_type' => 'entity' );
504
+
505
+        // Merge the defaults with the provided parameters.
506
+        $args = wp_parse_args( $params, $defaults );
507
+
508
+        // Call the `get_posts` function.
509
+        return get_posts( $args );
510
+    }
511 511
 
512 512
 }
Please login to merge, or discard this patch.