class-non-embedded-pdf-parser.php
987 Bytes
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
<?php
namespace WPML\Media\Classes;
use WPML\FP\Obj;
use WPML\FP\Str;
/**
* Media file block parser
*/
class WPML_Non_Embedded_Pdf_Parser extends WPML_Media_Element_Parser {
/**
* @var string
*/
private static $non_embedded_pdf_expression = '/wp:file.*class="wp-block-file".*(href=".*\.pdf")>.*\/wp:file/s';
public function getMediaElements() {
return $this->getFromTags();
}
public function getMediaSrcFromAttributes( $attrs ) {
return Obj::propOr( '', 'href', $attrs );
}
protected function getFromTags() {
// phpcs:disable WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
return preg_match_all( self::$non_embedded_pdf_expression, $this->blockText, $matches ) ?
$this->getAttachments( $matches ) : [];
}
/**
* Checks if media element is File Block and has pdf.
*
* @return bool
*/
public function validate() {
return Str::includes( '<!-- wp:file', $this->blockText )
|| Str::includes( 'pdf', $this->blockText );
}
}