Conditions | 12 |
Paths | 9 |
Total Lines | 75 |
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 |
||
73 | function jetpack_responsive_videos_maybe_wrap_oembed( $html, $url = null, $attr, $post_ID = null ) { |
||
74 | if ( empty( $html ) || ! is_string( $html ) || ! $url ) { |
||
75 | return $html; |
||
76 | } |
||
77 | |||
78 | $jetpack_video_wrapper = '<div class="jetpack-video-wrapper">'; |
||
79 | |||
80 | $already_wrapped = strpos( $html, $jetpack_video_wrapper ); |
||
81 | |||
82 | // If the oEmbed has already been wrapped, return the html. |
||
83 | if ( false !== $already_wrapped ) { |
||
84 | return $html; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * oEmbed Video Providers. |
||
89 | * |
||
90 | * A whitelist of oEmbed video provider Regex patterns to check against before wrapping the output. |
||
91 | * |
||
92 | * @module theme-tools |
||
93 | * |
||
94 | * @since 3.8.0 |
||
95 | * |
||
96 | * @param array $video_patterns oEmbed video provider Regex patterns. |
||
97 | */ |
||
98 | $video_patterns = apply_filters( |
||
99 | 'jetpack_responsive_videos_oembed_videos', |
||
100 | array( |
||
101 | 'https?://((m|www)\.)?youtube\.com/watch', |
||
102 | 'https?://((m|www)\.)?youtube\.com/playlist', |
||
103 | 'https?://youtu\.be/', |
||
104 | 'https?://(.+\.)?vimeo\.com/', |
||
105 | 'https?://(www\.)?dailymotion\.com/', |
||
106 | 'https?://dai.ly/', |
||
107 | 'https?://(www\.)?hulu\.com/watch/', |
||
108 | 'https?://wordpress.tv/', |
||
109 | 'https?://(www\.)?funnyordie\.com/videos/', |
||
110 | 'https?://vine.co/v/', |
||
111 | 'https?://(www\.)?collegehumor\.com/video/', |
||
112 | 'https?://(www\.|embed\.)?ted\.com/talks/', |
||
113 | ) |
||
114 | ); |
||
115 | |||
116 | // Merge patterns to run in a single preg_match call. |
||
117 | $video_patterns = '(' . implode( '|', $video_patterns ) . ')'; |
||
118 | |||
119 | $is_video = preg_match( $video_patterns, $url ); |
||
120 | |||
121 | /** |
||
122 | * Do we have info about the post? Let's check if it has a video block. |
||
123 | * This is only possible in the block editor. |
||
124 | */ |
||
125 | if ( |
||
126 | ! empty( $post_ID ) |
||
127 | && function_exists( 'parse_blocks' ) |
||
128 | ) { |
||
129 | $post_content = get_post_field( 'post_content', $post_ID ); |
||
130 | $post_blocks = parse_blocks( $post_content ); |
||
131 | if ( ! empty( $post_blocks ) ) { |
||
132 | foreach ( $post_blocks as $block ) { |
||
133 | // If we have embed blocks, do not apply responsive videos. |
||
134 | if ( false !== strpos( $block['blockName'], 'core-embed' ) ) { |
||
135 | return $html; |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | |||
141 | // If the oEmbed is a video, wrap it in the responsive wrapper. |
||
142 | if ( false === $already_wrapped && 1 === $is_video ) { |
||
143 | return jetpack_responsive_videos_embed_html( $html ); |
||
144 | } |
||
145 | |||
146 | return $html; |
||
147 | } |
||
148 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.