| Conditions | 67 |
| Paths | 16225 |
| Total Lines | 481 |
| Lines | 7 |
| Ratio | 1.46 % |
| 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 // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 180 | public function crowdsignal_shortcode( $atts ) { |
||
| 181 | global $post; |
||
| 182 | global $content_width; |
||
| 183 | |||
| 184 | if ( ! is_array( $atts ) ) { |
||
| 185 | return '<!-- Crowdsignal shortcode passed invalid attributes -->'; |
||
| 186 | } |
||
| 187 | |||
| 188 | $attributes = shortcode_atts( |
||
| 189 | array( |
||
| 190 | 'survey' => null, |
||
| 191 | 'link_text' => esc_html__( 'Take Our Survey', 'jetpack' ), |
||
| 192 | 'poll' => 'empty', |
||
| 193 | 'rating' => 'empty', |
||
| 194 | 'unique_id' => null, |
||
| 195 | 'item_id' => null, |
||
| 196 | 'title' => null, |
||
| 197 | 'permalink' => null, |
||
| 198 | 'cb' => 0, // cache buster. Helps with testing. |
||
| 199 | 'type' => 'button', |
||
| 200 | 'body' => '', |
||
| 201 | 'button' => '', |
||
| 202 | 'text_color' => '000000', |
||
| 203 | 'back_color' => 'FFFFFF', |
||
| 204 | 'align' => '', |
||
| 205 | 'style' => '', |
||
| 206 | 'width' => $content_width, |
||
| 207 | 'height' => floor( $content_width * 3 / 4 ), |
||
| 208 | 'delay' => 100, |
||
| 209 | 'visit' => 'single', |
||
| 210 | 'domain' => '', |
||
| 211 | 'id' => '', |
||
| 212 | 'site' => 'crowdsignal.com', |
||
| 213 | ), |
||
| 214 | $atts, |
||
| 215 | 'crowdsignal' |
||
| 216 | ); |
||
| 217 | |||
| 218 | $inline = ! in_the_loop() |
||
| 219 | && ! Constants::is_defined( 'TESTING_IN_JETPACK' ); |
||
| 220 | |||
| 221 | $no_script = false; |
||
| 222 | $infinite_scroll = false; |
||
| 223 | |||
| 224 | if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) { |
||
| 225 | $infinite_scroll = true; |
||
| 226 | } |
||
| 227 | |||
| 228 | if ( function_exists( 'get_option' ) && get_option( 'polldaddy_load_poll_inline' ) ) { |
||
| 229 | $inline = true; |
||
| 230 | } |
||
| 231 | |||
| 232 | if ( is_feed() || ( defined( 'DOING_AJAX' ) && ! $infinite_scroll ) ) { |
||
| 233 | $no_script = false; |
||
| 234 | } |
||
| 235 | |||
| 236 | self::$add_script = $infinite_scroll; |
||
| 237 | |||
| 238 | /* |
||
| 239 | * Rating embed. |
||
| 240 | */ |
||
| 241 | if ( (int) $attributes['rating'] > 0 && ! $no_script ) { |
||
| 242 | |||
| 243 | if ( empty( $attributes['unique_id'] ) ) { |
||
| 244 | $attributes['unique_id'] = is_page() ? 'wp-page-' . $post->ID : 'wp-post-' . $post->ID; |
||
| 245 | } |
||
| 246 | |||
| 247 | if ( empty( $attributes['item_id'] ) ) { |
||
| 248 | $attributes['item_id'] = is_page() ? '_page_' . $post->ID : '_post_' . $post->ID; |
||
| 249 | } |
||
| 250 | |||
| 251 | if ( empty( $attributes['title'] ) ) { |
||
| 252 | /** This filter is documented in core/src/wp-includes/general-template.php */ |
||
| 253 | $attributes['title'] = apply_filters( 'wp_title', $post->post_title, '', '' ); |
||
|
|
|||
| 254 | } |
||
| 255 | |||
| 256 | if ( empty( $attributes['permalink'] ) ) { |
||
| 257 | $attributes['permalink'] = get_permalink( $post->ID ); |
||
| 258 | } |
||
| 259 | |||
| 260 | $rating = (int) $attributes['rating']; |
||
| 261 | $unique_id = preg_replace( '/[^\-_a-z0-9]/i', '', wp_strip_all_tags( $attributes['unique_id'] ) ); |
||
| 262 | $item_id = wp_strip_all_tags( $attributes['item_id'] ); |
||
| 263 | $item_id = preg_replace( '/[^_a-z0-9]/i', '', $item_id ); |
||
| 264 | |||
| 265 | $settings = wp_json_encode( |
||
| 266 | array( |
||
| 267 | 'id' => $rating, |
||
| 268 | 'unique_id' => $unique_id, |
||
| 269 | 'title' => rawurlencode( trim( $attributes['title'] ) ), |
||
| 270 | 'permalink' => esc_url( $attributes['permalink'] ), |
||
| 271 | 'item_id' => $item_id, |
||
| 272 | ) |
||
| 273 | ); |
||
| 274 | |||
| 275 | $item_id = esc_js( $item_id ); |
||
| 276 | |||
| 277 | if ( |
||
| 278 | class_exists( 'Jetpack_AMP_Support' ) |
||
| 279 | && Jetpack_AMP_Support::is_amp_request() |
||
| 280 | ) { |
||
| 281 | return sprintf( |
||
| 282 | '<a href="%s" target="_blank">%s</a>', |
||
| 283 | esc_url( $attributes['permalink'] ), |
||
| 284 | esc_html( trim( $attributes['title'] ) ) |
||
| 285 | ); |
||
| 286 | } elseif ( $inline ) { |
||
| 287 | $rating_js = "<!--//--><![CDATA[//><!--\n"; |
||
| 288 | $rating_js .= "PDRTJS_settings_{$rating}{$item_id}={$settings};"; |
||
| 289 | $rating_js .= "\n//--><!]]>"; |
||
| 290 | |||
| 291 | wp_enqueue_script( 'crowdsignal-rating' ); |
||
| 292 | wp_add_inline_script( |
||
| 293 | 'crowdsignal-rating', |
||
| 294 | $rating_js, |
||
| 295 | 'before' |
||
| 296 | ); |
||
| 297 | |||
| 298 | return sprintf( |
||
| 299 | '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>', |
||
| 300 | absint( $rating ), |
||
| 301 | esc_attr( $item_id ) |
||
| 302 | ); |
||
| 303 | } else { |
||
| 304 | if ( false === self::$scripts ) { |
||
| 305 | self::$scripts = array(); |
||
| 306 | } |
||
| 307 | |||
| 308 | $data = array( |
||
| 309 | 'id' => $rating, |
||
| 310 | 'item_id' => $item_id, |
||
| 311 | 'settings' => $settings, |
||
| 312 | ); |
||
| 313 | |||
| 314 | self::$scripts['rating'][] = $data; |
||
| 315 | |||
| 316 | add_action( 'wp_footer', array( $this, 'generate_scripts' ) ); |
||
| 317 | |||
| 318 | if ( $infinite_scroll ) { |
||
| 319 | return sprintf( |
||
| 320 | '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s" data-settings="%3$s"></div>', |
||
| 321 | absint( $rating ), |
||
| 322 | esc_attr( $item_id ), |
||
| 323 | esc_attr( wp_json_encode( $data ) ) |
||
| 324 | ); |
||
| 325 | } else { |
||
| 326 | return sprintf( |
||
| 327 | '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>', |
||
| 328 | absint( $rating ), |
||
| 329 | esc_attr( $item_id ) |
||
| 330 | ); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | } elseif ( (int) $attributes['poll'] > 0 ) { |
||
| 334 | /* |
||
| 335 | * Poll embed. |
||
| 336 | */ |
||
| 337 | |||
| 338 | if ( empty( $attributes['title'] ) ) { |
||
| 339 | $attributes['title'] = esc_html__( 'Take Our Poll', 'jetpack' ); |
||
| 340 | } |
||
| 341 | |||
| 342 | $poll = (int) $attributes['poll']; |
||
| 343 | |||
| 344 | if ( 'crowdsignal.com' === $attributes['site'] ) { |
||
| 345 | $poll_url = sprintf( 'https://poll.fm/%d', $poll ); |
||
| 346 | } else { |
||
| 347 | $poll_url = sprintf( 'https://polldaddy.com/p/%d', $poll ); |
||
| 348 | } |
||
| 349 | |||
| 350 | $poll_js = sprintf( 'https://secure.polldaddy.com/p/%d.js', $poll ); |
||
| 351 | $poll_link = sprintf( |
||
| 352 | '<a href="%s" target="_blank">%s</a>', |
||
| 353 | esc_url( $poll_url ), |
||
| 354 | esc_html( $attributes['title'] ) |
||
| 355 | ); |
||
| 356 | |||
| 357 | if ( |
||
| 358 | $no_script |
||
| 359 | || ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) |
||
| 360 | ) { |
||
| 361 | return $poll_link; |
||
| 362 | } else { |
||
| 363 | /* |
||
| 364 | * Slider poll. |
||
| 365 | */ |
||
| 366 | if ( |
||
| 367 | 'slider' === $attributes['type'] |
||
| 368 | && ! $inline |
||
| 369 | ) { |
||
| 370 | |||
| 371 | if ( ! in_array( |
||
| 372 | $attributes['visit'], |
||
| 373 | array( 'single', 'multiple' ), |
||
| 374 | true |
||
| 375 | ) ) { |
||
| 376 | $attributes['visit'] = 'single'; |
||
| 377 | } |
||
| 378 | |||
| 379 | $settings = array( |
||
| 380 | 'type' => 'slider', |
||
| 381 | 'embed' => 'poll', |
||
| 382 | 'delay' => (int) $attributes['delay'], |
||
| 383 | 'visit' => $attributes['visit'], |
||
| 384 | 'id' => (int) $poll, |
||
| 385 | 'site' => $attributes['site'], |
||
| 386 | ); |
||
| 387 | |||
| 388 | return $this->get_async_code( $settings, $poll_link, $poll_url ); |
||
| 389 | } else { |
||
| 390 | if ( 1 === $attributes['cb'] ) { |
||
| 391 | $attributes['cb'] = '?cb=' . time(); |
||
| 392 | } else { |
||
| 393 | $attributes['cb'] = false; |
||
| 394 | } |
||
| 395 | $margins = ''; |
||
| 396 | $float = ''; |
||
| 397 | |||
| 398 | if ( in_array( |
||
| 399 | $attributes['align'], |
||
| 400 | array( 'right', 'left' ), |
||
| 401 | true |
||
| 402 | ) ) { |
||
| 403 | $float = sprintf( 'float: %s;', $attributes['align'] ); |
||
| 404 | |||
| 405 | if ( 'left' === $attributes['align'] ) { |
||
| 406 | $margins = 'margin: 0px 10px 0px 0px;'; |
||
| 407 | } elseif ( 'right' === $attributes['align'] ) { |
||
| 408 | $margins = 'margin: 0px 0px 0px 10px'; |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | /* |
||
| 413 | * Force the normal style embed on single posts/pages |
||
| 414 | * otherwise it's not rendered on infinite scroll themed blogs |
||
| 415 | * ('infinite_scroll_render' isn't fired) |
||
| 416 | */ |
||
| 417 | if ( is_singular() ) { |
||
| 418 | $inline = true; |
||
| 419 | } |
||
| 420 | |||
| 421 | if ( false === $attributes['cb'] && ! $inline ) { |
||
| 422 | if ( false === self::$scripts ) { |
||
| 423 | self::$scripts = array(); |
||
| 424 | } |
||
| 425 | |||
| 426 | $data = array( 'url' => $poll_js ); |
||
| 427 | |||
| 428 | self::$scripts['poll'][ (int) $poll ] = $data; |
||
| 429 | |||
| 430 | add_action( 'wp_footer', array( $this, 'generate_scripts' ) ); |
||
| 431 | |||
| 432 | wp_enqueue_script( 'crowdsignal-shortcode' ); |
||
| 433 | wp_localize_script( |
||
| 434 | 'crowdsignal-shortcode', |
||
| 435 | 'crowdsignal_shortcode_options', |
||
| 436 | array( |
||
| 437 | 'script_url' => esc_url_raw( |
||
| 438 | Assets::get_file_url_for_environment( |
||
| 439 | '_inc/build/polldaddy-shortcode.min.js', |
||
| 440 | '_inc/polldaddy-shortcode.js' |
||
| 441 | ) |
||
| 442 | ), |
||
| 443 | ) |
||
| 444 | ); |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Hook into the Crowdsignal shortcode before rendering. |
||
| 448 | * |
||
| 449 | * @since 8.4.0 |
||
| 450 | * |
||
| 451 | * @param int $poll Poll ID. |
||
| 452 | */ |
||
| 453 | do_action( 'crowdsignal_shortcode_before', (int) $poll ); |
||
| 454 | |||
| 455 | return sprintf( |
||
| 456 | '<a name="pd_a_%1$d"></a><div class="CSS_Poll PDS_Poll" id="PDI_container%1$d" data-settings="%2$s" style="%3$s%4$s"></div><div id="PD_superContainer"></div><noscript>%5$s</noscript>', |
||
| 457 | absint( $poll ), |
||
| 458 | esc_attr( wp_json_encode( $data ) ), |
||
| 459 | $float, |
||
| 460 | $margins, |
||
| 461 | $poll_link |
||
| 462 | ); |
||
| 463 | } else { |
||
| 464 | if ( $inline ) { |
||
| 465 | $attributes['cb'] = ''; |
||
| 466 | } |
||
| 467 | |||
| 468 | wp_enqueue_script( |
||
| 469 | 'crowdsignal-' . absint( $poll ), |
||
| 470 | esc_url( $poll_js . $attributes['cb'] ), |
||
| 471 | array(), |
||
| 472 | JETPACK__VERSION, |
||
| 473 | true |
||
| 474 | ); |
||
| 475 | |||
| 476 | /** This action is already documented in modules/shortcodes/crowdsignal.php */ |
||
| 477 | do_action( 'crowdsignal_shortcode_before', (int) $poll ); |
||
| 478 | |||
| 479 | return sprintf( |
||
| 480 | '<a id="pd_a_%1$s"></a><div class="CSS_Poll PDS_Poll" id="PDI_container%1$s" style="%2$s%3$s"></div><div id="PD_superContainer"></div><noscript>%4$s</noscript>', |
||
| 481 | absint( $poll ), |
||
| 482 | $float, |
||
| 483 | $margins, |
||
| 484 | $poll_link |
||
| 485 | ); |
||
| 486 | } |
||
| 487 | } |
||
| 488 | } |
||
| 489 | } elseif ( ! empty( $attributes['survey'] ) ) { |
||
| 490 | /* |
||
| 491 | * Survey embed. |
||
| 492 | */ |
||
| 493 | |||
| 494 | if ( in_array( |
||
| 495 | $attributes['type'], |
||
| 496 | array( 'iframe', 'button', 'banner', 'slider' ), |
||
| 497 | true |
||
| 498 | ) ) { |
||
| 499 | |||
| 500 | if ( empty( $attributes['title'] ) ) { |
||
| 501 | $attributes['title'] = esc_html__( 'Take Our Survey', 'jetpack' ); |
||
| 502 | if ( ! empty( $attributes['link_text'] ) ) { |
||
| 503 | $attributes['title'] = $attributes['link_text']; |
||
| 504 | } |
||
| 505 | } |
||
| 506 | |||
| 507 | if ( |
||
| 508 | 'banner' === $attributes['type'] |
||
| 509 | || 'slider' === $attributes['type'] |
||
| 510 | ) { |
||
| 511 | $inline = false; |
||
| 512 | } |
||
| 513 | |||
| 514 | $survey_url = ''; |
||
| 515 | |||
| 516 | if ( 'true' !== $attributes['survey'] ) { |
||
| 517 | $survey = preg_replace( '/[^a-f0-9]/i', '', $attributes['survey'] ); |
||
| 518 | |||
| 519 | if ( 'crowdsignal.com' === $attributes['site'] ) { |
||
| 520 | $survey_url = 'https://survey.fm/' . $survey; |
||
| 521 | } else { |
||
| 522 | $survey_url = 'https://polldaddy.com/s/' . $survey; |
||
| 523 | } |
||
| 524 | } else { |
||
| 525 | if ( isset( $attributes['domain'] ) && isset( $attributes['id'] ) ) { |
||
| 526 | $survey_url = 'https://' . $attributes['domain'] . '.survey.fm/' . $attributes['id']; |
||
| 527 | } |
||
| 528 | } |
||
| 529 | |||
| 530 | $survey_link = sprintf( |
||
| 531 | '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', |
||
| 532 | esc_url( $survey_url ), |
||
| 533 | esc_html( $attributes['title'] ) |
||
| 534 | ); |
||
| 535 | |||
| 536 | $settings = array(); |
||
| 537 | |||
| 538 | if ( 'iframe' === $attributes['type'] ) { |
||
| 539 | if ( 'auto' !== $attributes['height'] ) { |
||
| 540 | View Code Duplication | if ( |
|
| 541 | isset( $content_width ) |
||
| 542 | && is_numeric( $attributes['width'] ) |
||
| 543 | && $attributes['width'] > $content_width |
||
| 544 | ) { |
||
| 545 | $attributes['width'] = $content_width; |
||
| 546 | } |
||
| 547 | |||
| 548 | if ( ! $attributes['width'] ) { |
||
| 549 | $attributes['width'] = '100%'; |
||
| 550 | } else { |
||
| 551 | $attributes['width'] = (int) $attributes['width']; |
||
| 552 | } |
||
| 553 | |||
| 554 | if ( ! $attributes['height'] ) { |
||
| 555 | $attributes['height'] = '600'; |
||
| 556 | } else { |
||
| 557 | $attributes['height'] = (int) $attributes['height']; |
||
| 558 | } |
||
| 559 | |||
| 560 | return sprintf( |
||
| 561 | '<iframe src="%1$s?iframe=1" frameborder="0" width="%2$d" height="%3$d" scrolling="auto" allowtransparency="true" marginheight="0" marginwidth="0">%4$s</iframe>', |
||
| 562 | esc_url( $survey_url ), |
||
| 563 | absint( $attributes['width'] ), |
||
| 564 | absint( $attributes['height'] ), |
||
| 565 | $survey_link |
||
| 566 | ); |
||
| 567 | } elseif ( |
||
| 568 | ! empty( $attributes['domain'] ) |
||
| 569 | && ! empty( $attributes['id'] ) |
||
| 570 | ) { |
||
| 571 | $domain = preg_replace( '/[^a-z0-9\-]/i', '', $attributes['domain'] ); |
||
| 572 | $id = preg_replace( '/[\/\?&\{\}]/', '', $attributes['id'] ); |
||
| 573 | |||
| 574 | $auto_src = esc_url( "https://{$domain}.survey.fm/{$id}" ); |
||
| 575 | $auto_src = wp_parse_url( $auto_src ); |
||
| 576 | |||
| 577 | if ( ! is_array( $auto_src ) || 0 === count( $auto_src ) ) { |
||
| 578 | return '<!-- no crowdsignal output -->'; |
||
| 579 | } |
||
| 580 | |||
| 581 | if ( ! isset( $auto_src['host'] ) || ! isset( $auto_src['path'] ) ) { |
||
| 582 | return '<!-- no crowdsignal output -->'; |
||
| 583 | } |
||
| 584 | |||
| 585 | $domain = $auto_src['host'] . '/'; |
||
| 586 | $id = ltrim( $auto_src['path'], '/' ); |
||
| 587 | |||
| 588 | $settings = array( |
||
| 589 | 'type' => $attributes['type'], |
||
| 590 | 'auto' => true, |
||
| 591 | 'domain' => $domain, |
||
| 592 | 'id' => $id, |
||
| 593 | 'site' => $attributes['site'], |
||
| 594 | ); |
||
| 595 | } |
||
| 596 | } else { |
||
| 597 | $text_color = preg_replace( '/[^a-f0-9]/i', '', $attributes['text_color'] ); |
||
| 598 | $back_color = preg_replace( '/[^a-f0-9]/i', '', $attributes['back_color'] ); |
||
| 599 | |||
| 600 | if ( |
||
| 601 | ! in_array( |
||
| 602 | $attributes['align'], |
||
| 603 | array( |
||
| 604 | 'right', |
||
| 605 | 'left', |
||
| 606 | 'top-left', |
||
| 607 | 'top-right', |
||
| 608 | 'middle-left', |
||
| 609 | 'middle-right', |
||
| 610 | 'bottom-left', |
||
| 611 | 'bottom-right', |
||
| 612 | ), |
||
| 613 | true |
||
| 614 | ) |
||
| 615 | ) { |
||
| 616 | $attributes['align'] = ''; |
||
| 617 | } |
||
| 618 | |||
| 619 | if ( |
||
| 620 | ! in_array( |
||
| 621 | $attributes['style'], |
||
| 622 | array( |
||
| 623 | 'inline', |
||
| 624 | 'side', |
||
| 625 | 'corner', |
||
| 626 | 'rounded', |
||
| 627 | 'square', |
||
| 628 | ), |
||
| 629 | true |
||
| 630 | ) |
||
| 631 | ) { |
||
| 632 | $attributes['style'] = ''; |
||
| 633 | } |
||
| 634 | |||
| 635 | $settings = array_filter( |
||
| 636 | array( |
||
| 637 | 'title' => wp_strip_all_tags( $attributes['title'] ), |
||
| 638 | 'type' => $attributes['type'], |
||
| 639 | 'body' => wp_strip_all_tags( $attributes['body'] ), |
||
| 640 | 'button' => wp_strip_all_tags( $attributes['button'] ), |
||
| 641 | 'text_color' => $text_color, |
||
| 642 | 'back_color' => $back_color, |
||
| 643 | 'align' => $attributes['align'], |
||
| 644 | 'style' => $attributes['style'], |
||
| 645 | 'id' => $survey, |
||
| 646 | 'site' => $attributes['site'], |
||
| 647 | ) |
||
| 648 | ); |
||
| 649 | } |
||
| 650 | |||
| 651 | if ( empty( $settings ) ) { |
||
| 652 | return '<!-- no crowdsignal output -->'; |
||
| 653 | } |
||
| 654 | |||
| 655 | return $this->get_async_code( $settings, $survey_link, $survey_url ); |
||
| 656 | } |
||
| 657 | } else { |
||
| 658 | return '<!-- no crowdsignal output -->'; |
||
| 659 | } |
||
| 660 | } |
||
| 661 | |||
| 777 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.