PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / averta / wordpress / src / Utility / Extract.php

Extract.php in Depicter — Popup & Slider Builder trunk, at vendor/averta/wordpress/src/Utility/Extract.php

107 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Averta\WordPress\Utility;
3
4
5 class Extract
6 {
7 /**
8 * Extract all images from content
9 *
10 * @param string $text The text to extract images from.
11 *
12 * @return bool|array List of images in array
13 */
14 public static function imagesFromText( $text )
15 {
16 preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $text, $matches );
17 return isset( $matches ) && count( $matches[0] ) ? $matches : false;
18 }
19
20 /**
21 * Get first image src from content
22 *
23 * @param string $text The content to extract image from.
24 *
25 * @return string First image URL on success and empty string if nothing found
26 */
27 public static function firstImageSrcFromText( $text )
28 {
29 $images = self::imagesFromText( $text );
30
31 return ( $images && count( $images[1]) ) ? $images[1][0] : '';
32 }
33
34 /**
35 * Get first image tag from string
36 *
37 * @param string $text The text to extract image from.
38 *
39 * @return string First image tag on success and empty string if nothing found
40 */
41 public static function firstImageFromText( $text )
42 {
43 $images = self::imagesFromText( $text );
44
45 return ( $images && count( $images[0]) ) ? $images[0][0] : '';
46 }
47
48
49 /**
50 * Crawls the content and checks if the shortcode is present in the content or not
51 * - If list of $attributes was passed, it tries to extract and collect the value of attributes
52 * - and returns an array containing the values
53 *
54 * @param string $content Content to search for shortcodes.
55 * @param string $shortcodeName Shortcode tag name to check.
56 * @param array $attributes List of shortcode attribute which should be extracted
57 *
58 * @return array|bool False if shortcode does not exist in the content.
59 * An array containing attribute values if $attributes is not empty and shortcode exists in the content
60 * True if shortcode exits in the content
61 */
62 public static function shortcodeAttributes( $content, $shortcodeName, $attributes = [] ){
63 $hasShortcode = false;
64
65 if ( false === strpos( $content, '[' ) ) {
66 return false;
67 }
68 preg_match_all( '/' . get_shortcode_regex( [ $shortcodeName ] ) . '/', $content, $matches, PREG_SET_ORDER );
69
70 if ( empty( $matches ) ) {
71 return false;
72 }
73
74 $extractedAttributes = [];
75
76 foreach ( $matches as $shortcode ) {
77 // if shortcode found in the content
78 if ( $shortcodeName === $shortcode[2] ) {
79 $hasShortcode = $hasShortcode || 1;
80
81 if( empty( $attributes ) && $hasShortcode ){
82 return true;
83 }
84
85 if( empty( $shortcode[3] ) ){
86 continue;
87 }
88
89 if( is_array( $attributes ) ){
90 foreach( $attributes as $attributeName ){
91 preg_match( '/'. $attributeName .'=["|\']([^\"\']+)["|\']/m', $shortcode[3], $attrMatches, PREG_OFFSET_CAPTURE, 0);
92 if( !empty( $attrMatches[1][0] ) ){
93 $extractedAttributes[ $attributeName ][] = $attrMatches[1][0];
94 }
95 }
96 }
97 }
98 }
99
100 if( !empty( $extractedAttributes ) ){
101 return $extractedAttributes;
102 }
103
104 return $hasShortcode;
105 }
106 }
107