@@ -1140,21 +1140,173 @@ function utopia_get_page_title( $post = null ) {
1140
1140
1141
1141
1142
1142
function utopia_wrap_long_text ( $ text ) {
1143
- if ( strlen ( $ text ) > 685 ) {
1144
- $ trimmed_text = substr ( $ text , 0 , 685 );
1145
- $ last_space_position = strrpos ( $ trimmed_text , ' ' );
1143
+ // new
1144
+ if ( ! $ text ) {
1145
+ return ;
1146
+ }
1146
1147
1147
- if ( false !== $ last_space_position ) {
1148
- $ trimmed_text = substr ( $ trimmed_text , 0 , $ last_space_position );
1148
+ $ max_length = 685 ;
1149
+
1150
+ // Используем DOMDocument для корректной работы с HTML
1151
+ $ dom = new DOMDocument ();
1152
+ libxml_use_internal_errors ( true );
1153
+ $ dom ->loadHTML ( mb_convert_encoding ( $ text , 'HTML-ENTITIES ' , 'UTF-8 ' ) );
1154
+ libxml_clear_errors ();
1155
+
1156
+ $ body = $ dom ->getElementsByTagName ( 'body ' )->item ( 0 );
1157
+ $ truncated_content = '' ;
1158
+ $ remaining_content = '' ;
1159
+ $ current_length = 0 ;
1160
+ $ truncated = false ;
1161
+ $ nodes_to_be_visible = array ();
1162
+ $ nodes_to_be_invisible = array ();
1163
+
1164
+ foreach ( $ body ->childNodes as $ node ) {
1165
+ if ( $ node ->nodeType !== XML_ELEMENT_NODE ) {
1166
+ continue ;
1149
1167
}
1150
1168
1151
- $ remaining_text = substr ( $ text , $ last_space_position );
1152
- $ concert_more_text = '<span class="concert-more-text" hidden> ' . $ remaining_text . '</span> ' ;
1153
- $ concert_more_button = ' <button class="concert-more-button" onclick="this.previousElementSibling.hidden = false; this.hidden = true;"> ' . pll__ ( 'more ' ) . '</button> ' ;
1154
- return $ trimmed_text . $ concert_more_text . $ concert_more_button ;
1155
- } else {
1156
- return $ text ;
1169
+ $ node_content = $ dom ->saveHTML ( $ node );
1170
+ $ node_length = mb_strlen ( strip_tags ( $ node_content ) );
1171
+
1172
+ if ( $ current_length + $ node_length < $ max_length ) {
1173
+ $ nodes_to_be_visible [] = $ node ;
1174
+ } else {
1175
+ $ truncated = true ;
1176
+ $ nodes_to_be_invisible [] = $ node ;
1177
+ }
1178
+
1179
+ $ current_length += $ node_length ;
1180
+ }
1181
+
1182
+ // $nodes_to_be_visible = array_filter(
1183
+ // $nodes_to_be_visible,
1184
+ // function ( $node ) {
1185
+ // return $node->nodeType === XML_ELEMENT_NODE;
1186
+ // }
1187
+ // );
1188
+ // $nodes_to_be_invisible = array_filter(
1189
+ // $nodes_to_be_visible,
1190
+ // function ( $node ) {
1191
+ // return $node->nodeType === XML_ELEMENT_NODE;
1192
+ // }
1193
+ // );
1194
+
1195
+ // var_dump(
1196
+ // array_map(
1197
+ // function ( $node ) use ( $dom ) {
1198
+ // return $dom->saveHTML( $node );
1199
+ // },
1200
+ // $nodes_to_be_visible
1201
+ // ),
1202
+ // array_map(
1203
+ // function ( $node ) use ( $dom ) {
1204
+ // return $dom->saveHTML( $node );
1205
+ // },
1206
+ // $nodes_to_be_invisible
1207
+ // )
1208
+ // );
1209
+ // var_dump(
1210
+ // $nodes_to_be_visible,
1211
+ // $nodes_to_be_invisible
1212
+ // );
1213
+
1214
+ if ( $ truncated ) {
1215
+ $ last_visible_node = end ( $ nodes_to_be_visible );
1216
+
1217
+ $ node_content = strip_tags ( $ dom ->saveHTML ( $ last_visible_node ) );
1218
+ $ last_char = $ node_content [ strlen ( $ node_content ) - 1 ];
1219
+
1220
+ if ( ! preg_match ( '/\s/ ' , $ last_char ) ) {
1221
+ $ last_visible_node ->appendChild ( $ dom ->createTextNode ( ' ' ) );
1222
+ }
1223
+
1224
+ $ button = $ dom ->createElement ( 'button ' , pll__ ( 'more ' ) );
1225
+ $ button ->setAttribute ( 'class ' , 'concert-more-button ' );
1226
+ $ button ->setAttribute ( 'onclick ' , 'this.parentElement.nextElementSibling.hidden = false; this.hidden = true; ' );
1227
+ $ last_visible_node ->appendChild ( $ button );
1228
+ }
1229
+
1230
+ foreach ( $ nodes_to_be_visible as $ node ) {
1231
+ $ truncated_content .= $ dom ->saveHTML ( $ node );
1157
1232
}
1233
+
1234
+ foreach ( $ nodes_to_be_invisible as $ node ) {
1235
+ $ remaining_content .= $ dom ->saveHTML ( $ node );
1236
+ }
1237
+
1238
+ return $ truncated_content . '<div hidden> ' . $ remaining_content . '</div> ' ;
1239
+
1240
+ foreach ( $ body ->childNodes as $ node ) {
1241
+ if ( $ truncated ) {
1242
+ $ remaining_content .= $ dom ->saveHTML ( $ node );
1243
+ } else {
1244
+ $ node_content = $ dom ->saveHTML ( $ node );
1245
+ $ node_length = mb_strlen ( strip_tags ( $ node_content ) );
1246
+
1247
+ if ( $ current_length + $ node_length > $ max_length ) {
1248
+ $ truncated = true ;
1249
+ $ button = $ dom ->createElement ( 'button ' , pll__ ( 'more ' ) );
1250
+ $ button ->setAttribute ( 'class ' , 'concert-more-button ' );
1251
+ $ node ->appendChild ( $ button );
1252
+
1253
+ $ remaining_content .= $ node_content ;
1254
+ } else {
1255
+ $ truncated_content .= $ node_content ;
1256
+ $ current_length += $ node_length ;
1257
+ }
1258
+ }
1259
+ }
1260
+
1261
+ // var_dump( $truncated_content, $remaining_content );
1262
+
1263
+ echo '<div class="acf-content"> ' ;
1264
+ echo $ truncated_content ;
1265
+
1266
+ if ( $ remaining_content ) {
1267
+ echo '<div class="remaining-content" style="display: none;"> ' . $ remaining_content . '</div> ' ;
1268
+ }
1269
+
1270
+ echo '</div> ' ;
1271
+
1272
+ // // JavaScript для обработки клика по кнопке
1273
+ // echo "
1274
+ // <script>
1275
+ // document.addEventListener('DOMContentLoaded', function() {
1276
+ // var button = document.querySelector('.show-more');
1277
+ // var remainingContent = document.querySelector('.remaining-content');
1278
+
1279
+ // if (button && remainingContent) {
1280
+ // button.addEventListener('click', function() {
1281
+ // if (remainingContent.style.display === 'none') {
1282
+ // remainingContent.style.display = 'block';
1283
+ // button.textContent = 'Show less';
1284
+ // } else {
1285
+ // remainingContent.style.display = 'none';
1286
+ // button.textContent = 'Show more';
1287
+ // }
1288
+ // });
1289
+ // }
1290
+ // });
1291
+ // </script>
1292
+ // ";
1293
+
1294
+ // old
1295
+ // if ( strlen( $text ) > 685 ) {
1296
+ // $trimmed_text = substr( $text, 0, 685 );
1297
+ // $last_space_position = strrpos( $trimmed_text, ' ' );
1298
+
1299
+ // if ( false !== $last_space_position ) {
1300
+ // $trimmed_text = substr( $trimmed_text, 0, $last_space_position );
1301
+ // }
1302
+
1303
+ // $remaining_text = substr( $text, $last_space_position );
1304
+ // $concert_more_text = '<span class="concert-more-text" hidden>' . $remaining_text . '</span>';
1305
+ // $concert_more_button = ' <button class="concert-more-button" onclick="this.previousElementSibling.hidden = false; this.hidden = true;">' . pll__( 'more' ) . '</button>';
1306
+ // return $trimmed_text . $concert_more_text . $concert_more_button;
1307
+ // } else {
1308
+ // return $text;
1309
+ // }
1158
1310
}
1159
1311
1160
1312
function utopia_get_language_switcher () {
0 commit comments