class-wpml-media-href-parser.php
1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace WPML\Media\Classes;
use WPML\FP\Obj;
use WPML\FP\Str;
/**
* Media in href parser (basically for files included in classic editor)
*/
class WPML_Media_Href_Parser extends WPML_Media_Element_Parser {
private static $allAnchorsRegex = '/<a.*?>.*?<\/a>/s'; // gets any anchor tag
private static $hrefElementRegex = '/<a ([^>]+)/s'; // to get matches for anchors after filtering them (filtering means that we get only anchors without nested tags)
public function getMediaElements() {
return $this->getFromTags();
}
public function getMediaSrcFromAttributes( $attrs ) {
return Obj::propOr('', 'href', $attrs);
}
protected function getFromTags() {
$anchorsWithoutTags = $this->getAnchorsWithoutNestedTags();
return preg_match_all( self::$hrefElementRegex, implode( '', $anchorsWithoutTags ), $matches ) ? $this->getAttachments( $matches ) : [];
}
/**
* Checks if media element is only anchor with href (basically for files uploaded in classic editor).
*
* @return bool
*/
public function validate() {
return Str::includes( '<a href=', $this->blockText ) && ! empty( $this->getAnchorsWithoutNestedTags() );
}
/**
* Gets anchor tags from WP editor that contain neither nested tags not 'wp-block' string in it.
*
* @return array
*/
public function getAnchorsWithoutNestedTags() {
$anchorHasNestedTags = function ( $anchorTag ) {
$pattern = '/<a .*?>.*?<.*?<\/a>/s';
preg_match( $pattern, $anchorTag, $matches );
return ! empty( $matches );
};
$isBlockAnchor = Str::includes( 'wp-block' );
preg_match_all( self::$allAnchorsRegex, $this->blockText, $allAnchorTags );
return wpml_collect( current( $allAnchorTags ) )
->reject( $anchorHasNestedTags )
->reject( $isBlockAnchor )
->toArray();
}
}