Ticket #31093: trac_31093_has_shortcode_array_support.diff
File trac_31093_has_shortcode_array_support.diff, 2.5 KB (added by , 10 years ago) |
---|
-
wp-includes/shortcodes.php
diff --git wp-includes/shortcodes.php wp-includes/shortcodes.php index 1919af4..814e653 100644
function shortcode_exists( $tag ) { 141 141 } 142 142 143 143 /** 144 * Whether the passed content contains the specified shortcode 144 * Whether the passed content contains any or specified shortcode 145 * Can accept an array of shortcode tags 145 146 * 146 147 * @since 3.6.0 147 148 * 148 149 * @global array $shortcode_tags 149 150 * 150 151 * @param string $content Content to search for shortcodes. 151 * @param string $tag Shortcode tag to check. 152 * @param array | string $tag Optional. Shortcode tag to check. 153 * @param string When multiple shortcode tags are passed, comparison operator for final result. 152 154 * @return bool Whether the passed content contains the given shortcode. 153 155 */ 154 function has_shortcode( $content, $tag ) {156 function has_shortcode( $content, $tag = '', $tag_relation = 'OR' ) { 155 157 if ( false === strpos( $content, '[' ) ) { 156 158 return false; 157 159 } 158 160 159 if ( shortcode_exists( $tag ) ) { 160 preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ); 161 if ( empty( $matches ) ) 162 return false; 161 preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ); 162 if ( empty( $matches ) ) 163 return false; 164 165 if ( empty( $tag ) ) { 166 return true; 167 } 168 169 if ( is_array( $tag ) ) { 170 $tag_relation = strtoupper( $tag_relation ); 171 172 if ( $tag_relation == 'OR' ) { 173 // Return true as soon as the first shortcode tag is found 174 foreach ( $tag as $stag ) { 175 foreach ( $matches as $shortcode ) { 176 if ( $stag === $shortcode[2] ) { 177 return true; 178 } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $stag ) ) { 179 return true; 180 } 181 } 182 } 183 } else if ( $tag_relation == 'AND' ) { 184 // Collect existence of all shortcode tags 185 $found_shortcode = array(); 186 foreach ( $tag as $stag ) { 187 $found_shortcode[ $stag ] = false; 188 foreach ( $matches as $shortcode ) { 189 if ( $stag === $shortcode[2] ) { 190 $found_shortcode[ $stag ] = true; 191 } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $stag ) ) { 192 $found_shortcode[ $stag ] = true; 193 } 194 } 195 } 163 196 197 // return true if all are true, else false 198 return count( array_unique( $found_shortcode ) ) == 1 && current( $found_shortcode ); 199 } 200 } else { 164 201 foreach ( $matches as $shortcode ) { 165 202 if ( $tag === $shortcode[2] ) { 166 203 return true; … … function has_shortcode( $content, $tag ) { 169 206 } 170 207 } 171 208 } 209 172 210 return false; 173 211 } 174 212