| Conditions | 8 |
| Paths | 4 |
| Total Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 136 | public function get_jsonld( $is_homepage = false, $post_id = null ) { |
||
| 137 | |||
| 138 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
||
| 139 | // e-mails with a low apdex score. |
||
| 140 | // |
||
| 141 | // See https://github.com/insideout10/wordlift-plugin/issues/521 |
||
| 142 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
||
| 143 | |||
| 144 | // Switch to Website converter if is home page. |
||
| 145 | if ( $is_homepage ) { |
||
| 146 | /** |
||
| 147 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
||
| 148 | * |
||
| 149 | * @since 3.14.0 |
||
| 150 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
||
| 151 | */ |
||
| 152 | if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
||
| 153 | return array(); |
||
| 154 | } |
||
| 155 | |||
| 156 | // Set a reference to the website_converter. |
||
| 157 | $website_converter = $this->website_converter; |
||
| 158 | |||
| 159 | // Send JSON-LD. |
||
| 160 | return $website_converter->create_schema(); |
||
| 161 | } |
||
| 162 | |||
| 163 | // If no id has been provided return an empty array. |
||
| 164 | if ( ! isset( $post_id ) ) { |
||
| 165 | return array(); |
||
| 166 | } |
||
| 167 | |||
| 168 | // An array of references which is captured when converting an URI to a |
||
| 169 | // json which we gather to further expand our json-ld. |
||
| 170 | $references = array(); |
||
| 171 | $references_infos = array(); |
||
| 172 | |||
| 173 | // Set a reference to the entity_to_jsonld_converter to use in the closures. |
||
| 174 | $entity_to_jsonld_converter = $this->converter; |
||
| 175 | |||
| 176 | // Convert each URI to a JSON-LD array, while gathering referenced entities. |
||
| 177 | // in the references array. |
||
| 178 | $jsonld = array_merge( |
||
| 179 | array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) ), |
||
| 180 | // Convert each URI in the references array to JSON-LD. We don't output |
||
| 181 | // entities already output above (hence the array_diff). |
||
| 182 | array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references_infos ) { |
||
| 183 | |||
| 184 | // "2nd level properties" may not output here, e.g. a post |
||
| 185 | // mentioning an event, located in a place: the place is referenced |
||
| 186 | // via the `@id` but no other properties are loaded. |
||
| 187 | $ignored = array(); |
||
| 188 | |||
| 189 | return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos ); |
||
| 190 | }, array_unique( $references ) ) ) ); |
||
| 191 | |||
| 192 | $required_references = array_filter( $references_infos, function ( $item ) use ( $references ) { |
||
| 193 | return isset( $item['reference'] ) && |
||
| 194 | // Check that the reference is required |
||
| 195 | $item['reference']->get_required() && |
||
| 196 | // Check that the reference isn't being output already. |
||
| 197 | ! in_array( $item['reference']->get_id(), $references ); |
||
| 198 | } ); |
||
| 199 | |||
| 200 | $jsonld = array_merge( $jsonld, array_filter( array_map( function ( $item ) use ( $references, $entity_to_jsonld_converter ) { |
||
| 201 | |||
| 202 | if ( ! isset( $item['reference'] ) ) { |
||
| 203 | return null; |
||
| 204 | } |
||
| 205 | |||
| 206 | $post_id = $item['reference']->get_id(); |
||
| 207 | if ( in_array( $post_id, $references ) ) { |
||
| 208 | return null; |
||
| 209 | } |
||
| 210 | |||
| 211 | $references[] = $post_id; |
||
| 212 | |||
| 213 | return $entity_to_jsonld_converter->convert( $post_id, $references ); |
||
| 214 | }, $required_references ) ) ); |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Filter name: wl_after_get_jsonld |
||
| 218 | * @return array |
||
| 219 | * @since 3.27.2 |
||
| 220 | * @var $jsonld array The final jsonld before outputting to page. |
||
| 221 | * @var $post_id int The post id for which the jsonld is generated. |
||
| 222 | * |
||
| 223 | */ |
||
| 224 | $jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id ); |
||
| 225 | |||
| 226 | return $jsonld; |
||
| 227 | } |
||
| 228 | |||
| 250 |
If you suppress an error, we recommend checking for the error condition explicitly: