Completed
Push — master ( 676e7c...b3b410 )
by David
01:03
created
src/public/class-wordlift-related-entities-cloud-shortcode.php 2 patches
Indentation   +152 added lines, -152 removed lines patch added patch discarded remove patch
@@ -16,164 +16,164 @@
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Related_Entities_Cloud_Shortcode extends Wordlift_Shortcode {
18 18
 
19
-	/**
20
-	 * {@inheritdoc}
21
-	 */
22
-	const SHORTCODE = 'wl_cloud';
23
-
24
-	/**
25
-	 * The {@link Wordlift_Relation_Service} instance.
26
-	 *
27
-	 * @since  3.15.0
28
-	 * @access private
29
-	 * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
30
-	 */
31
-	private $relation_service;
32
-	/**
33
-	 * @var Wordlift_Entity_Service
34
-	 */
35
-	private $entity_service;
36
-
37
-	/**
38
-	 * Create a {@link Wordlift_Related_Entities_Cloud_Shortcode} instance.
39
-	 *
40
-	 * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
41
-	 * @param \Wordlift_Entity_Service $entity_service
42
-	 *
43
-	 * @since 3.15.0
44
-	 *
45
-	 */
46
-	public function __construct( $relation_service, $entity_service ) {
47
-		parent::__construct();
48
-
49
-		$this->relation_service = $relation_service;
50
-
51
-		$this->entity_service = $entity_service;
52
-
53
-		$this->register_block_type();
54
-
55
-	}
56
-
57
-	/**
58
-	 * {@inheritdoc}
59
-	 */
60
-	public function render( $atts ) {
61
-
62
-		$tags = $this->get_related_entities_tags();
63
-
64
-		// Bail out if there are no associated entities.
65
-		if ( empty( $tags ) ) {
66
-			return '';
67
-		}
68
-
69
-		/*
19
+    /**
20
+     * {@inheritdoc}
21
+     */
22
+    const SHORTCODE = 'wl_cloud';
23
+
24
+    /**
25
+     * The {@link Wordlift_Relation_Service} instance.
26
+     *
27
+     * @since  3.15.0
28
+     * @access private
29
+     * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
30
+     */
31
+    private $relation_service;
32
+    /**
33
+     * @var Wordlift_Entity_Service
34
+     */
35
+    private $entity_service;
36
+
37
+    /**
38
+     * Create a {@link Wordlift_Related_Entities_Cloud_Shortcode} instance.
39
+     *
40
+     * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
41
+     * @param \Wordlift_Entity_Service $entity_service
42
+     *
43
+     * @since 3.15.0
44
+     *
45
+     */
46
+    public function __construct( $relation_service, $entity_service ) {
47
+        parent::__construct();
48
+
49
+        $this->relation_service = $relation_service;
50
+
51
+        $this->entity_service = $entity_service;
52
+
53
+        $this->register_block_type();
54
+
55
+    }
56
+
57
+    /**
58
+     * {@inheritdoc}
59
+     */
60
+    public function render( $atts ) {
61
+
62
+        $tags = $this->get_related_entities_tags();
63
+
64
+        // Bail out if there are no associated entities.
65
+        if ( empty( $tags ) ) {
66
+            return '';
67
+        }
68
+
69
+        /*
70 70
 		 * Since the output is use in the widget as well, we need to have the
71 71
 		 * same class as the core tagcloud widget, to easily inherit its styling.
72 72
 		 */
73 73
 
74
-		return '<div class="tagcloud wl-related-entities-cloud">' .
75
-		       wp_generate_tag_cloud( $tags, $atts ) .
76
-		       '</div>';
77
-	}
78
-
79
-	private function register_block_type() {
80
-
81
-		$scope = $this;
82
-
83
-		add_action( 'init', function () use ( $scope ) {
84
-			if ( ! function_exists( 'register_block_type' ) ) {
85
-				// Gutenberg is not active.
86
-				return;
87
-			}
88
-
89
-			register_block_type( 'wordlift/cloud', array(
90
-				'editor_script'   => 'wl-block-editor',
91
-				'render_callback' => function ( $attributes ) use ( $scope ) {
92
-					$attr_code = '';
93
-					foreach ( $attributes as $key => $value ) {
94
-						$attr_code .= $key . '="' . htmlentities( $value ) . '" ';
95
-					}
96
-
97
-					return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']';
98
-				},
99
-				'attributes'      => array(
100
-					'preview'     => array(
101
-						'type'    => 'boolean',
102
-						'default' => false,
103
-					),
104
-					'preview_src' => array(
105
-						'type'    => 'string',
106
-						'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/cloud.png',
107
-					),
108
-				),
109
-			) );
110
-		} );
111
-	}
112
-
113
-	/**
114
-	 * Find the related entities to the currently displayed post and
115
-	 * calculate the "tags" for them as wp_generate_tag_cloud expects to get.
116
-	 *
117
-	 * @return array    Array of tags. Empty array in case we re not in a context
118
-	 *                  of a post, or it has no related entities.
119
-	 * @since 3.11.0
120
-	 *
121
-	 */
122
-	public function get_related_entities_tags() {
123
-
124
-		// Define the supported types list.
125
-		$supported_types = Wordlift_Entity_Service::valid_entity_post_types();
126
-
127
-		// Show nothing if not on a post or entity page.
128
-		if ( ! is_singular( $supported_types ) ) {
129
-			return array();
130
-		}
131
-
132
-		// Get the IDs of entities related to current post.
133
-		$related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) );
134
-
135
-		// Bail out if there are no associated entities.
136
-		if ( empty( $related_entities ) ) {
137
-			return array();
138
-		}
139
-
140
-		/*
74
+        return '<div class="tagcloud wl-related-entities-cloud">' .
75
+                wp_generate_tag_cloud( $tags, $atts ) .
76
+                '</div>';
77
+    }
78
+
79
+    private function register_block_type() {
80
+
81
+        $scope = $this;
82
+
83
+        add_action( 'init', function () use ( $scope ) {
84
+            if ( ! function_exists( 'register_block_type' ) ) {
85
+                // Gutenberg is not active.
86
+                return;
87
+            }
88
+
89
+            register_block_type( 'wordlift/cloud', array(
90
+                'editor_script'   => 'wl-block-editor',
91
+                'render_callback' => function ( $attributes ) use ( $scope ) {
92
+                    $attr_code = '';
93
+                    foreach ( $attributes as $key => $value ) {
94
+                        $attr_code .= $key . '="' . htmlentities( $value ) . '" ';
95
+                    }
96
+
97
+                    return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']';
98
+                },
99
+                'attributes'      => array(
100
+                    'preview'     => array(
101
+                        'type'    => 'boolean',
102
+                        'default' => false,
103
+                    ),
104
+                    'preview_src' => array(
105
+                        'type'    => 'string',
106
+                        'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/cloud.png',
107
+                    ),
108
+                ),
109
+            ) );
110
+        } );
111
+    }
112
+
113
+    /**
114
+     * Find the related entities to the currently displayed post and
115
+     * calculate the "tags" for them as wp_generate_tag_cloud expects to get.
116
+     *
117
+     * @return array    Array of tags. Empty array in case we re not in a context
118
+     *                  of a post, or it has no related entities.
119
+     * @since 3.11.0
120
+     *
121
+     */
122
+    public function get_related_entities_tags() {
123
+
124
+        // Define the supported types list.
125
+        $supported_types = Wordlift_Entity_Service::valid_entity_post_types();
126
+
127
+        // Show nothing if not on a post or entity page.
128
+        if ( ! is_singular( $supported_types ) ) {
129
+            return array();
130
+        }
131
+
132
+        // Get the IDs of entities related to current post.
133
+        $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) );
134
+
135
+        // Bail out if there are no associated entities.
136
+        if ( empty( $related_entities ) ) {
137
+            return array();
138
+        }
139
+
140
+        /*
141 141
 		 * Create an array of "tags" to feed to wp_generate_tag_cloud.
142 142
 		 * Use the number of posts and entities connected to the entity as a weight.
143 143
 		 */
144
-		$tags = array();
145
-
146
-		foreach ( array_unique( $related_entities ) as $entity_id ) {
147
-
148
-			$connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) );
149
-			$connected_posts    = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) );
150
-			/**
151
-			 * @since 3.31.5
152
-			 * if synonym exists, use it instead of entity name.
153
-			 */
154
-			$synonyms    = $this->get_synonyms( $entity_id );
155
-			$entity_name = count( $synonyms ) > 0 ? $synonyms[0] : get_the_title( $entity_id );
156
-
157
-			$tags[] = (object) array(
158
-				'id'    => $entity_id,
159
-				// Used to give a unique class on the tag.
160
-				'name'  => $entity_name,
161
-				// The text of the tag.
162
-				'slug'  => get_the_title( $entity_id ),
163
-				// Required but not seem to be relevant
164
-				'link'  => get_permalink( $entity_id ),
165
-				// the url the tag links to.
166
-				'count' => $connected_entities + $connected_posts,
167
-				// The weight.
168
-			);
169
-
170
-		}
171
-
172
-		return $tags;
173
-	}
174
-
175
-	private function get_synonyms( $entity_id ) {
176
-		return $this->entity_service->get_alternative_labels( $entity_id );
177
-	}
144
+        $tags = array();
145
+
146
+        foreach ( array_unique( $related_entities ) as $entity_id ) {
147
+
148
+            $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) );
149
+            $connected_posts    = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) );
150
+            /**
151
+             * @since 3.31.5
152
+             * if synonym exists, use it instead of entity name.
153
+             */
154
+            $synonyms    = $this->get_synonyms( $entity_id );
155
+            $entity_name = count( $synonyms ) > 0 ? $synonyms[0] : get_the_title( $entity_id );
156
+
157
+            $tags[] = (object) array(
158
+                'id'    => $entity_id,
159
+                // Used to give a unique class on the tag.
160
+                'name'  => $entity_name,
161
+                // The text of the tag.
162
+                'slug'  => get_the_title( $entity_id ),
163
+                // Required but not seem to be relevant
164
+                'link'  => get_permalink( $entity_id ),
165
+                // the url the tag links to.
166
+                'count' => $connected_entities + $connected_posts,
167
+                // The weight.
168
+            );
169
+
170
+        }
171
+
172
+        return $tags;
173
+    }
174
+
175
+    private function get_synonyms( $entity_id ) {
176
+        return $this->entity_service->get_alternative_labels( $entity_id );
177
+    }
178 178
 
179 179
 }
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
 	 * @since 3.15.0
44 44
 	 *
45 45
 	 */
46
-	public function __construct( $relation_service, $entity_service ) {
46
+	public function __construct($relation_service, $entity_service) {
47 47
 		parent::__construct();
48 48
 
49 49
 		$this->relation_service = $relation_service;
@@ -57,12 +57,12 @@  discard block
 block discarded – undo
57 57
 	/**
58 58
 	 * {@inheritdoc}
59 59
 	 */
60
-	public function render( $atts ) {
60
+	public function render($atts) {
61 61
 
62 62
 		$tags = $this->get_related_entities_tags();
63 63
 
64 64
 		// Bail out if there are no associated entities.
65
-		if ( empty( $tags ) ) {
65
+		if (empty($tags)) {
66 66
 			return '';
67 67
 		}
68 68
 
@@ -71,8 +71,8 @@  discard block
 block discarded – undo
71 71
 		 * same class as the core tagcloud widget, to easily inherit its styling.
72 72
 		 */
73 73
 
74
-		return '<div class="tagcloud wl-related-entities-cloud">' .
75
-		       wp_generate_tag_cloud( $tags, $atts ) .
74
+		return '<div class="tagcloud wl-related-entities-cloud">'.
75
+		       wp_generate_tag_cloud($tags, $atts).
76 76
 		       '</div>';
77 77
 	}
78 78
 
@@ -80,21 +80,21 @@  discard block
 block discarded – undo
80 80
 
81 81
 		$scope = $this;
82 82
 
83
-		add_action( 'init', function () use ( $scope ) {
84
-			if ( ! function_exists( 'register_block_type' ) ) {
83
+		add_action('init', function() use ($scope) {
84
+			if ( ! function_exists('register_block_type')) {
85 85
 				// Gutenberg is not active.
86 86
 				return;
87 87
 			}
88 88
 
89
-			register_block_type( 'wordlift/cloud', array(
89
+			register_block_type('wordlift/cloud', array(
90 90
 				'editor_script'   => 'wl-block-editor',
91
-				'render_callback' => function ( $attributes ) use ( $scope ) {
91
+				'render_callback' => function($attributes) use ($scope) {
92 92
 					$attr_code = '';
93
-					foreach ( $attributes as $key => $value ) {
94
-						$attr_code .= $key . '="' . htmlentities( $value ) . '" ';
93
+					foreach ($attributes as $key => $value) {
94
+						$attr_code .= $key.'="'.htmlentities($value).'" ';
95 95
 					}
96 96
 
97
-					return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']';
97
+					return '['.$scope::SHORTCODE.' '.$attr_code.']';
98 98
 				},
99 99
 				'attributes'      => array(
100 100
 					'preview'     => array(
@@ -103,10 +103,10 @@  discard block
 block discarded – undo
103 103
 					),
104 104
 					'preview_src' => array(
105 105
 						'type'    => 'string',
106
-						'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/cloud.png',
106
+						'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/cloud.png',
107 107
 					),
108 108
 				),
109
-			) );
109
+			));
110 110
 		} );
111 111
 	}
112 112
 
@@ -125,15 +125,15 @@  discard block
 block discarded – undo
125 125
 		$supported_types = Wordlift_Entity_Service::valid_entity_post_types();
126 126
 
127 127
 		// Show nothing if not on a post or entity page.
128
-		if ( ! is_singular( $supported_types ) ) {
128
+		if ( ! is_singular($supported_types)) {
129 129
 			return array();
130 130
 		}
131 131
 
132 132
 		// Get the IDs of entities related to current post.
133
-		$related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) );
133
+		$related_entities = wl_core_get_related_entity_ids(get_the_ID(), array('status' => 'publish'));
134 134
 
135 135
 		// Bail out if there are no associated entities.
136
-		if ( empty( $related_entities ) ) {
136
+		if (empty($related_entities)) {
137 137
 			return array();
138 138
 		}
139 139
 
@@ -143,25 +143,25 @@  discard block
 block discarded – undo
143 143
 		 */
144 144
 		$tags = array();
145 145
 
146
-		foreach ( array_unique( $related_entities ) as $entity_id ) {
146
+		foreach (array_unique($related_entities) as $entity_id) {
147 147
 
148
-			$connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) );
149
-			$connected_posts    = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) );
148
+			$connected_entities = count(wl_core_get_related_entity_ids($entity_id, array('status' => 'publish')));
149
+			$connected_posts    = count($this->relation_service->get_article_subjects($entity_id, '*', null, 'publish'));
150 150
 			/**
151 151
 			 * @since 3.31.5
152 152
 			 * if synonym exists, use it instead of entity name.
153 153
 			 */
154
-			$synonyms    = $this->get_synonyms( $entity_id );
155
-			$entity_name = count( $synonyms ) > 0 ? $synonyms[0] : get_the_title( $entity_id );
154
+			$synonyms    = $this->get_synonyms($entity_id);
155
+			$entity_name = count($synonyms) > 0 ? $synonyms[0] : get_the_title($entity_id);
156 156
 
157 157
 			$tags[] = (object) array(
158 158
 				'id'    => $entity_id,
159 159
 				// Used to give a unique class on the tag.
160 160
 				'name'  => $entity_name,
161 161
 				// The text of the tag.
162
-				'slug'  => get_the_title( $entity_id ),
162
+				'slug'  => get_the_title($entity_id),
163 163
 				// Required but not seem to be relevant
164
-				'link'  => get_permalink( $entity_id ),
164
+				'link'  => get_permalink($entity_id),
165 165
 				// the url the tag links to.
166 166
 				'count' => $connected_entities + $connected_posts,
167 167
 				// The weight.
@@ -172,8 +172,8 @@  discard block
 block discarded – undo
172 172
 		return $tags;
173 173
 	}
174 174
 
175
-	private function get_synonyms( $entity_id ) {
176
-		return $this->entity_service->get_alternative_labels( $entity_id );
175
+	private function get_synonyms($entity_id) {
176
+		return $this->entity_service->get_alternative_labels($entity_id);
177 177
 	}
178 178
 
179 179
 }
Please login to merge, or discard this patch.