PluginProbe
WPSSO Core – Complete Schema Markup and Meta Tags / trunk
WPSSO Core – Complete Schema Markup and Meta Tags vtrunk
22.6.1 22.6.0 22.5.3 22.5.2 22.5.1 22.4.0 22.3.0 22.2.1 22.2.0 22.1.3 22.1.2 22.1.1 22.1.0 22.0.0 21.13.3 21.13.2 trunk 21.13.1
wpsso / lib / com / gettext.php

gettext.php in WPSSO Core – Complete Schema Markup and Meta Tags trunk, at lib/com/gettext.php

147 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * License: GPLv3
4 * License URI: https://www.gnu.org/licenses/gpl.txt
5 * Copyright 2024-2026 Jean-Sebastien Morisset (https://surniaulula.com/)
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9
10 die( 'These aren\'t the droids you\'re looking for.' );
11 }
12
13 if ( ! class_exists( 'SucomGetText' ) ) {
14
15 class SucomGetText {
16
17 /*
18 * Translate HTML headers, paragraphs, list items, and blockquotes.
19 */
20 public static function get_html_transl( $html, $text_domain ) {
21
22 $gettext = self::parse_html( $html, $text_domain );
23
24 foreach ( $gettext as $match => $arr ) {
25
26 $transl = _x( $arr[ 'text' ], $arr[ 'context' ], $arr[ 'text_domain' ] );
27
28 $html = str_replace( $match, $arr[ 'begin' ] . $transl . $arr[ 'end' ], $html );
29 }
30
31 return $html;
32 }
33
34 public static function show_html_php( $html, $text_domain ) {
35
36 $gettext = self::parse_html( $html, $text_domain );
37
38 foreach ( $gettext as $match => $arr ) {
39
40 $arr[ 'text' ] = str_replace( '\'', '\\\'', $arr[ 'text' ] );
41
42 echo sprintf( '_x( \'%s\', \'%s\', \'%s\' );', $arr[ 'text' ], $arr[ 'context' ], $arr[ 'text_domain' ] ) . "\n";
43 }
44 }
45
46 public static function show_lib_php( $mixed, $context, $text_domain ) {
47
48 if ( is_array( $mixed ) ) {
49
50 foreach ( $mixed as $key => $val ) {
51
52 if ( 'admin' === $key ) {
53
54 continue;
55 }
56
57 self::show_lib_php( $val, $context, $text_domain );
58 }
59
60 return;
61
62 } elseif ( is_numeric( $mixed ) ) { // Number.
63
64 return;
65
66 } elseif ( empty( $mixed ) ) { // Empty.
67
68 return;
69
70 } elseif ( 0 === strpos( $mixed, '/' ) ) { // Regular expression.
71
72 return;
73
74 } elseif ( false !== filter_var( $mixed, FILTER_VALIDATE_URL ) ) { // URL is valid.
75
76 return;
77 }
78
79 $mixed = str_replace( '\'', '\\\'', $mixed );
80
81 echo sprintf( '_x( \'%s\', \'%s\', \'%s\' );', $mixed, $context, $text_domain ) . "\n";
82
83 /*
84 * Include values without their comment / qualifier (for example, 'Adult (13 years old or more)').
85 */
86 if ( 'option value' === $context ) {
87
88 if ( false !== ( $pos = strpos( $mixed, '(' ) ) ) {
89
90 $mixed = trim( substr( $mixed, 0, $pos ) );
91
92 echo sprintf( '_x( \'%s\', \'%s\', \'%s\' );', $mixed, $context, $text_domain ) . "\n";
93 }
94
95 if ( 0 === strpos( $mixed, '[' ) ) {
96
97 $mixed = trim( $mixed, '[]' );
98
99 echo sprintf( '_x( \'%s\', \'%s\', \'%s\' );', $mixed, $context, $text_domain ) . "\n";
100 }
101 }
102 }
103
104 private static function parse_html( $html, $text_domain ) {
105
106 $parsed = array();
107
108 foreach ( array(
109 '/(<h[0-9][^>]*>)(.*)(<\/h[0-9]>)/Uis' => 'html header',
110 '/(<p>|<p [^>]*>)(.*)(<\/p>)/Uis' => 'html paragraph', // Get paragraphs before list items.
111 '/(<li[^>]*>)(.*)(<\/li>)/Uis' => 'html list item',
112 '/(<blockquote[^>]*>)(.*)(<\/blockquote>)/Uis' => 'html blockquote',
113 ) as $pattern => $context ) {
114
115 if ( preg_match_all( $pattern, $html, $all_matches, PREG_SET_ORDER ) ) {
116
117 foreach ( $all_matches as $num => $matches ) {
118
119 list( $match, $begin, $text, $end ) = $matches;
120
121 $html = str_replace( $match, '', $html ); // Do not match again.
122
123 $text = trim( $text ); // Just in case.
124
125 if ( '' === $text ) { // Ignore HTML tags with no content.
126
127 continue;
128 }
129
130 $text = preg_replace( '/[\s\n\r]+/s', ' ', $text ); // Put everything on one line.
131
132 $parsed[ $match ] = array(
133 'begin' => $begin,
134 'text' => $text,
135 'end' => $end,
136 'context' => $context,
137 'text_domain' => $text_domain,
138 );
139 }
140 }
141 }
142
143 return $parsed;
144 }
145 }
146 }
147