Changeset 3173455
- Timestamp:
- 10/22/2024 08:33:54 AM (17 months ago)
- Location:
- quickcreator
- Files:
-
- 6 edited
- 1 copied
-
tags/0.1.0 (copied) (copied from quickcreator/trunk)
-
tags/0.1.0/includes/quickcreator/content-parsers/class-gutenberg-parser.php (modified) (23 diffs)
-
tags/0.1.0/quickcreator.php (modified) (1 diff)
-
tags/0.1.0/readme.txt (modified) (1 diff)
-
trunk/includes/quickcreator/content-parsers/class-gutenberg-parser.php (modified) (23 diffs)
-
trunk/quickcreator.php (modified) (1 diff)
-
trunk/readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
quickcreator/tags/0.1.0/includes/quickcreator/content-parsers/class-gutenberg-parser.php
r3142565 r3173455 1 1 <?php 2 2 3 /** 3 4 * Parser that prepare data for Gutenberg … … 14 15 * Object that imports data from different sources into WordPress. 15 16 */ 16 class Gutenberg_Parser extends Content_Parser { 17 class Gutenberg_Parser extends Content_Parser 18 { 17 19 18 20 /** … … 22 24 * @return string 23 25 */ 24 public function parse_content( $content ) { 25 26 parent::parse_content( $content ); 27 28 $content = wp_unslash( $content ); 26 public function parse_content($content) 27 { 28 29 parent::parse_content($content); 30 31 $content = wp_unslash($content); 29 32 30 33 $doc = new DOMDocument(); … … 33 36 $utf8_fix_suffix = '</body></html>'; 34 37 35 $doc->loadHTML( $utf8_fix_prefix . $content . $utf8_fix_suffix, LIBXML_HTML_NODEFDTD | LIBXML_SCHEMA_CREATE);38 $doc->loadHTML($utf8_fix_prefix . $content . $utf8_fix_suffix, LIBXML_HTML_NODEFDTD | LIBXML_SCHEMA_CREATE); 36 39 37 40 $parsed_content = ''; 38 41 39 $this->parse_dom_node( $doc, $parsed_content);42 $this->parse_dom_node($doc, $parsed_content); 40 43 41 44 return $parsed_content; … … 49 52 * @return void 50 53 */ 51 private function parse_dom_node( $parent_node, &$content ) { 54 private function parse_dom_node($parent_node, &$content) 55 { 52 56 // @codingStandardsIgnoreLine 53 foreach ( $parent_node->childNodes as $node) {57 foreach ($parent_node->childNodes as $node) { 54 58 55 59 // @codingStandardsIgnoreLine 56 $execute_for_child = $this->check_if_execute_recurrence( $node->nodeName);57 58 $content .= $this->parse_certain_node_type( $node);59 60 if ( $execute_for_child && $node->hasChildNodes()) {61 $this->parse_dom_node( $node, $content);60 $execute_for_child = $this->check_if_execute_recurrence($node->nodeName); 61 62 $content .= $this->parse_certain_node_type($node); 63 64 if ($execute_for_child && $node->hasChildNodes()) { 65 $this->parse_dom_node($node, $content); 62 66 } 63 67 } … … 70 74 * @return bool 71 75 */ 72 private function check_if_execute_recurrence( $node_type ) { 76 private function check_if_execute_recurrence($node_type) 77 { 73 78 74 79 $execute_for_child = true; 75 80 76 if ( 'ul' === $node_type) {81 if ('ul' === $node_type) { 77 82 $execute_for_child = false; 78 83 } 79 84 80 if ( 'ol' === $node_type) {85 if ('ol' === $node_type) { 81 86 $execute_for_child = false; 82 87 } 83 88 84 if ( 'blockquote' === $node_type) {89 if ('blockquote' === $node_type) { 85 90 $execute_for_child = false; 86 91 } 87 92 88 if ( 'table' === $node_type) {93 if ('table' === $node_type) { 89 94 $execute_for_child = false; 90 95 } … … 99 104 * @return string 100 105 */ 101 private function parse_certain_node_type( $node ) { 102 103 $all_attributes = $this->parse_node_attributes( $node ); 106 private function parse_certain_node_type($node) 107 { 108 109 $all_attributes = $this->parse_node_attributes($node); 104 110 $attributes = $all_attributes['attributes']; 105 111 … … 108 114 109 115 $gutenberg_attribute = ''; 110 if ( isset( $all_attributes['gutenberg_block_specific'][ $node_name ] ) ) { 111 $gutenberg_attribute = $all_attributes['gutenberg_block_specific'][ $node_name ]; 112 } 113 114 if ( 'p' === $node_name ) { 115 return $this->parse_node_p( $node, $attributes, $gutenberg_attribute ); 116 } 117 118 if ( 0 === strpos( $node_name, 'h' ) && 'html' !== $node_name && 'hr' !== $node_name && 'head' !== $node_name ) { 119 return $this->parse_node_h( $node, $attributes, $gutenberg_attribute ); 120 } 121 122 if ( 'img' === $node_name ) { 123 return $this->parse_node_img( $node, $attributes, $gutenberg_attribute ); 124 } 125 126 if ( 'ul' === $node_name ) { 127 return $this->parse_node_ul( $node, $attributes, $gutenberg_attribute ); 128 } 129 130 if ( 'ol' === $node_name ) { 131 return $this->parse_node_ol( $node, $attributes, $gutenberg_attribute ); 132 } 133 134 if ( 'hr' === $node_name ) { 135 return $this->parse_node_hr( $node, $attributes, $gutenberg_attribute ); 136 } 137 138 if ( 'blockquote' === $node_name ) { 139 return $this->parse_node_blockquote( $node, $attributes, $gutenberg_attribute ); 140 } 141 142 if ( 'pre' === $node_name ) { 143 return $this->parse_node_code( $node, $attributes, $gutenberg_attribute ); 144 } 145 146 if ( 'table' === $node_name ) { 147 return $this->parse_node_table( $node, $attributes, $gutenberg_attribute); 116 if (isset($all_attributes['gutenberg_block_specific'][$node_name])) { 117 $gutenberg_attribute = $all_attributes['gutenberg_block_specific'][$node_name]; 118 } 119 120 if ('p' === $node_name) { 121 return $this->parse_node_p($node, $attributes, $gutenberg_attribute); 122 } 123 124 if (0 === strpos($node_name, 'h') && 'html' !== $node_name && 'hr' !== $node_name && 'head' !== $node_name) { 125 return $this->parse_node_h($node, $attributes, $gutenberg_attribute); 126 } 127 128 if ('img' === $node_name) { 129 return $this->parse_node_img($node, $attributes, $gutenberg_attribute); 130 } 131 132 if ('ul' === $node_name) { 133 return $this->parse_node_ul($node, $attributes, $gutenberg_attribute); 134 } 135 136 if ('ol' === $node_name) { 137 return $this->parse_node_ol($node, $attributes, $gutenberg_attribute); 138 } 139 140 if ('hr' === $node_name) { 141 return $this->parse_node_hr($node, $attributes, $gutenberg_attribute); 142 } 143 144 if ('blockquote' === $node_name) { 145 return $this->parse_node_blockquote($node, $attributes, $gutenberg_attribute); 146 } 147 148 if ('pre' === $node_name) { 149 return $this->parse_node_code($node, $attributes, $gutenberg_attribute); 150 } 151 152 if ('table' === $node_name) { 153 return $this->parse_node_table($node, $attributes, $gutenberg_attribute); 154 } 155 156 if ('iframe' === $node_name) { 157 return $this->parse_node_iframe($node, $attributes, $gutenberg_attribute); 148 158 } 149 159 150 160 return ''; 151 161 } 162 152 163 153 164 /** … … 157 168 * @return array 158 169 */ 159 private function parse_node_attributes( $node ) { 170 private function parse_node_attributes($node) 171 { 160 172 161 173 $attributes_array = array(); … … 163 175 $attr_value = ''; 164 176 165 if ( $node->hasAttributes()) {177 if ($node->hasAttributes()) { 166 178 167 179 // @codingStandardsIgnoreLine 168 180 $node_name = $node->nodeName; 169 181 170 foreach ( $node->attributes as $attr) {182 foreach ($node->attributes as $attr) { 171 183 172 184 // @codingStandardsIgnoreLine … … 175 187 $attr_value = $attr->nodeValue; 176 188 177 if ( 'contenteditable' === $attr_name || 'class' === $attr_name) {189 if ('contenteditable' === $attr_name || 'class' === $attr_name) { 178 190 continue; 179 191 } 180 192 181 $attributes_array[ $attr_name] = $attr_value;193 $attributes_array[$attr_name] = $attr_value; 182 194 } 183 195 184 if ( in_array( $node_name, array( 'h2', 'h3', 'h4', 'h5', 'h6', 'h7' ), true ) && isset( $attr_name ) && 'style' === $attr_name) {185 $this->parse_h_special_attributes( $attr_value, $attributes_array, $block_specific_attributes);196 if (in_array($node_name, array('h2', 'h3', 'h4', 'h5', 'h6', 'h7'), true) && isset($attr_name) && 'style' === $attr_name) { 197 $this->parse_h_special_attributes($attr_value, $attributes_array, $block_specific_attributes); 186 198 } 187 199 188 if ( isset( $attr_name ) && 'p' === $node_name && 'style' === $attr_name) {189 $this->parse_p_special_attributes( $attr_value, $attributes_array, $block_specific_attributes);200 if (isset($attr_name) && 'p' === $node_name && 'style' === $attr_name) { 201 $this->parse_p_special_attributes($attr_value, $attributes_array, $block_specific_attributes); 190 202 } 191 203 } … … 204 216 * @param array $block_specific_attributes - reference to final array of gutenberg attributes. 205 217 */ 206 private function parse_p_special_attributes( $styles_string, &$attributes_array, &$block_specific_attributes ) { 207 208 $styles = explode( ';', $styles_string ); 218 private function parse_p_special_attributes($styles_string, &$attributes_array, &$block_specific_attributes) 219 { 220 221 $styles = explode(';', $styles_string); 209 222 $styles_assoc = array(); 210 foreach ( $styles as $style) {211 $s = explode( ':', $style);212 $styles_assoc[ $s[0] ] = trim( $s[1]);213 } 214 215 if ( key_exists( 'text-align', $styles_assoc )) {223 foreach ($styles as $style) { 224 $s = explode(':', $style); 225 $styles_assoc[$s[0]] = trim($s[1]); 226 } 227 228 if (key_exists('text-align', $styles_assoc)) { 216 229 $block_specific_attributes['p'] = '{"align":"' . $styles_assoc['text-align'] . '"} '; 217 230 } … … 225 238 * @param array $block_specific_attributes - reference to final array of gutenberg attributes. 226 239 */ 227 private function parse_h_special_attributes( $styles_string, &$attributes_array, &$block_specific_attributes ) { 228 229 $styles = explode( ';', $styles_string ); 240 private function parse_h_special_attributes($styles_string, &$attributes_array, &$block_specific_attributes) 241 { 242 243 $styles = explode(';', $styles_string); 230 244 $styles_assoc = array(); 231 foreach ( $styles as $style) {232 $s = explode( ':', $style);233 $styles_assoc[ $s[0] ] = trim( $s[1]);234 } 235 236 if ( key_exists( 'text-align', $styles_assoc )) {237 238 $styles_assoc['text-align'] = str_replace( 'start', 'left', $styles_assoc['text-align']);239 $styles_assoc['text-align'] = str_replace( 'end', 'right', $styles_assoc['text-align']);245 foreach ($styles as $style) { 246 $s = explode(':', $style); 247 $styles_assoc[$s[0]] = trim($s[1]); 248 } 249 250 if (key_exists('text-align', $styles_assoc)) { 251 252 $styles_assoc['text-align'] = str_replace('start', 'left', $styles_assoc['text-align']); 253 $styles_assoc['text-align'] = str_replace('end', 'right', $styles_assoc['text-align']); 240 254 241 255 $block_specific_attributes['h'] = '"textAlign":"' . $styles_assoc['text-align'] . '"'; 242 256 243 if ( ! isset( $attributes_array['class'] )) {257 if (! isset($attributes_array['class'])) { 244 258 $attributes_array['class'] = ''; 245 259 } 246 260 247 261 $attributes_array['class'] .= ' has-text-align-' . $styles_assoc['text-align']; 248 unset( $attributes_array['style']);262 unset($attributes_array['style']); 249 263 } 250 264 } … … 258 272 * @return string 259 273 */ 260 private function parse_node_p( $node, $attributes, $gutenberg_attribute ) { 261 262 $attributes = $this->glue_attributes( $attributes ); 263 264 $node_content = $this->get_inner_html( $node ); 265 if ( '' === $node_content ) { 266 return ''; 267 } 268 269 $attributes = str_replace( 'has-text-align-start', '', $attributes ); 274 private function parse_node_p($node, $attributes, $gutenberg_attribute) 275 { 276 277 $attributes = $this->glue_attributes($attributes); 278 279 $node_content = $this->get_inner_html($node); 280 if ('' === $node_content) { 281 return ''; 282 } 283 284 $attributes = str_replace('has-text-align-start', '', $attributes); 270 285 271 286 $content = '<!-- wp:paragraph ' . $gutenberg_attribute . '-->' . PHP_EOL; … … 284 299 * @return string 285 300 */ 286 private function parse_node_h( $node, $attributes, $gutenberg_attribute ) { 287 288 $h1_text = $this->get_inner_html( $node ); 301 private function parse_node_h($node, $attributes, $gutenberg_attribute) 302 { 303 304 $h1_text = $this->get_inner_html($node); 289 305 290 306 // @codingStandardsIgnoreLine 291 307 $node_name = $node->nodeName; 292 308 293 if ( 'h1' === $node_name && wp_strip_all_tags( $h1_text ) === $this->title) {294 return ''; 295 } 296 297 $attributes = $this->glue_attributes( $attributes);298 299 $header_size = str_replace( 'h', '', $node_name);309 if ('h1' === $node_name && wp_strip_all_tags($h1_text) === $this->title) { 310 return ''; 311 } 312 313 $attributes = $this->glue_attributes($attributes); 314 315 $header_size = str_replace('h', '', $node_name); 300 316 301 317 $content = '<!-- wp:heading {"level":' . $header_size . '} -->' . PHP_EOL; … … 306 322 } 307 323 324 325 /** 326 * Parse <iframe> node 327 * @param DOMNode|DOMDocument|DOMElement $node - node to parse. 328 * @param array $attributes - HTML attributes for the node, example: class="" src="". 329 * @param string $gutenberg_attribute - Attributes for Gutenberg tag. 330 * @return string 331 */ 332 private function parse_node_iframe($node, $attributes, $gutenberg_attribute) 333 { 334 335 $url = ''; 336 337 if (isset($attributes['src']) && ! empty($attributes['src'])) { 338 $url = $attributes['src']; 339 } 340 341 342 $content = '<!-- wp:embed {"url":"' . $url . '","type":"rich","providerNameSlug":"embed-handler","responsive":true,"className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} -->' . PHP_EOL; 343 $content .= 344 '<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">' . PHP_EOL; 345 $content .= $url . PHP_EOL; 346 $content .= '</div></figure>' . PHP_EOL; 347 $content .= '<!-- /wp:embed -->' . PHP_EOL . PHP_EOL; 348 349 return $content; 350 } 351 308 352 /** 309 353 * Parses <img> node. … … 314 358 * @return string 315 359 */ 316 private function parse_node_img( $node, $attributes, $gutenberg_attribute ) { 360 private function parse_node_img($node, $attributes, $gutenberg_attribute) 361 { 317 362 318 363 $image_url = ''; 319 364 $image_alt = ''; 320 365 321 if ( isset( $attributes['src'] ) && ! empty( $attributes['src'] )) {366 if (isset($attributes['src']) && ! empty($attributes['src'])) { 322 367 $image_url = $attributes['src']; 323 368 } 324 369 325 if ( isset( $attributes['alt'] ) && ! empty( $attributes['alt'] )) {370 if (isset($attributes['alt']) && ! empty($attributes['alt'])) { 326 371 $image_alt = $attributes['alt']; 327 372 } 328 373 329 $image_url = $this->download_img_to_media_library( $image_url, $image_alt);374 $image_url = $this->download_img_to_media_library($image_url, $image_alt); 330 375 $attributes['src'] = $image_url; 331 376 332 $attributes = $this->glue_attributes( $attributes);377 $attributes = $this->glue_attributes($attributes); 333 378 334 379 $content = '<!-- wp:image -->' . PHP_EOL; … … 349 394 * @return string 350 395 */ 351 private function parse_node_ul( $node, $attributes, $gutenberg_attribute ) { 352 353 $node_content = $this->get_inner_html( $node ); 354 if ( '' === $node_content ) { 396 private function parse_node_ul($node, $attributes, $gutenberg_attribute) 397 { 398 399 $node_content = $this->get_inner_html($node); 400 if ('' === $node_content) { 355 401 return ''; 356 402 } … … 372 418 * @param string $gutenberg_attribute - Attributes for Gutenberg tag. 373 419 */ 374 private function parse_node_table($node, $attributes, $gutenberg_attribute) { 375 376 $node_content = $this->get_inner_html( $node ); 377 if ( '' === $node_content ) { 420 private function parse_node_table($node, $attributes, $gutenberg_attribute) 421 { 422 423 $node_content = $this->get_inner_html($node); 424 if ('' === $node_content) { 378 425 return ''; 379 426 } … … 398 445 * @return string 399 446 */ 400 private function parse_node_ol( $node, $attributes, $gutenberg_attribute ) { 401 402 $node_content = $this->get_inner_html( $node ); 403 if ( '' === $node_content ) { 404 return ''; 405 } 406 407 $attributes = $this->glue_attributes( $attributes ); 447 private function parse_node_ol($node, $attributes, $gutenberg_attribute) 448 { 449 450 $node_content = $this->get_inner_html($node); 451 if ('' === $node_content) { 452 return ''; 453 } 454 455 $attributes = $this->glue_attributes($attributes); 408 456 409 457 $content = '<!-- wp:list {"ordered":true} -->' . PHP_EOL; … … 424 472 * @return string 425 473 */ 426 private function parse_node_hr( $node, $attributes, $gutenberg_attribute ) { 427 428 $attributes = $this->glue_attributes( $attributes ); 474 private function parse_node_hr($node, $attributes, $gutenberg_attribute) 475 { 476 477 $attributes = $this->glue_attributes($attributes); 429 478 430 479 $content = '<!-- wp:separator -->' . PHP_EOL; … … 443 492 * @return string 444 493 */ 445 private function parse_node_blockquote( $node, $attributes, $gutenberg_attribute ) { 446 447 $node_content = $this->get_inner_html( $node ); 448 if ( '' === $node_content ) { 494 private function parse_node_blockquote($node, $attributes, $gutenberg_attribute) 495 { 496 497 $node_content = $this->get_inner_html($node); 498 if ('' === $node_content) { 449 499 return ''; 450 500 } … … 465 515 * @return string 466 516 */ 467 private function parse_node_code( $node, $attributes, $gutenberg_attribute ) { 468 469 $node_content = $this->get_inner_html( $node ); 470 if ( '' === $node_content ) { 517 private function parse_node_code($node, $attributes, $gutenberg_attribute) 518 { 519 520 $node_content = $this->get_inner_html($node); 521 if ('' === $node_content) { 471 522 return ''; 472 523 } -
quickcreator/tags/0.1.0/quickcreator.php
r3167407 r3173455 4 4 * Plugin URI: https://wordpress.org/plugins/quickcreator/ 5 5 * Description: Create post with Quickcreator in WordPress 6 * Version: 0. 0.96 * Version: 0.1.0 7 7 * Author: Quickcreator 8 8 * Author URI: https://quickcreator.io -
quickcreator/tags/0.1.0/readme.txt
r3167407 r3173455 5 5 Requires PHP: 7.4 6 6 Tested up to: 6.6 7 Stable tag: 0. 0.97 Stable tag: 0.1.0 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html -
quickcreator/trunk/includes/quickcreator/content-parsers/class-gutenberg-parser.php
r3142565 r3173455 1 1 <?php 2 2 3 /** 3 4 * Parser that prepare data for Gutenberg … … 14 15 * Object that imports data from different sources into WordPress. 15 16 */ 16 class Gutenberg_Parser extends Content_Parser { 17 class Gutenberg_Parser extends Content_Parser 18 { 17 19 18 20 /** … … 22 24 * @return string 23 25 */ 24 public function parse_content( $content ) { 25 26 parent::parse_content( $content ); 27 28 $content = wp_unslash( $content ); 26 public function parse_content($content) 27 { 28 29 parent::parse_content($content); 30 31 $content = wp_unslash($content); 29 32 30 33 $doc = new DOMDocument(); … … 33 36 $utf8_fix_suffix = '</body></html>'; 34 37 35 $doc->loadHTML( $utf8_fix_prefix . $content . $utf8_fix_suffix, LIBXML_HTML_NODEFDTD | LIBXML_SCHEMA_CREATE);38 $doc->loadHTML($utf8_fix_prefix . $content . $utf8_fix_suffix, LIBXML_HTML_NODEFDTD | LIBXML_SCHEMA_CREATE); 36 39 37 40 $parsed_content = ''; 38 41 39 $this->parse_dom_node( $doc, $parsed_content);42 $this->parse_dom_node($doc, $parsed_content); 40 43 41 44 return $parsed_content; … … 49 52 * @return void 50 53 */ 51 private function parse_dom_node( $parent_node, &$content ) { 54 private function parse_dom_node($parent_node, &$content) 55 { 52 56 // @codingStandardsIgnoreLine 53 foreach ( $parent_node->childNodes as $node) {57 foreach ($parent_node->childNodes as $node) { 54 58 55 59 // @codingStandardsIgnoreLine 56 $execute_for_child = $this->check_if_execute_recurrence( $node->nodeName);57 58 $content .= $this->parse_certain_node_type( $node);59 60 if ( $execute_for_child && $node->hasChildNodes()) {61 $this->parse_dom_node( $node, $content);60 $execute_for_child = $this->check_if_execute_recurrence($node->nodeName); 61 62 $content .= $this->parse_certain_node_type($node); 63 64 if ($execute_for_child && $node->hasChildNodes()) { 65 $this->parse_dom_node($node, $content); 62 66 } 63 67 } … … 70 74 * @return bool 71 75 */ 72 private function check_if_execute_recurrence( $node_type ) { 76 private function check_if_execute_recurrence($node_type) 77 { 73 78 74 79 $execute_for_child = true; 75 80 76 if ( 'ul' === $node_type) {81 if ('ul' === $node_type) { 77 82 $execute_for_child = false; 78 83 } 79 84 80 if ( 'ol' === $node_type) {85 if ('ol' === $node_type) { 81 86 $execute_for_child = false; 82 87 } 83 88 84 if ( 'blockquote' === $node_type) {89 if ('blockquote' === $node_type) { 85 90 $execute_for_child = false; 86 91 } 87 92 88 if ( 'table' === $node_type) {93 if ('table' === $node_type) { 89 94 $execute_for_child = false; 90 95 } … … 99 104 * @return string 100 105 */ 101 private function parse_certain_node_type( $node ) { 102 103 $all_attributes = $this->parse_node_attributes( $node ); 106 private function parse_certain_node_type($node) 107 { 108 109 $all_attributes = $this->parse_node_attributes($node); 104 110 $attributes = $all_attributes['attributes']; 105 111 … … 108 114 109 115 $gutenberg_attribute = ''; 110 if ( isset( $all_attributes['gutenberg_block_specific'][ $node_name ] ) ) { 111 $gutenberg_attribute = $all_attributes['gutenberg_block_specific'][ $node_name ]; 112 } 113 114 if ( 'p' === $node_name ) { 115 return $this->parse_node_p( $node, $attributes, $gutenberg_attribute ); 116 } 117 118 if ( 0 === strpos( $node_name, 'h' ) && 'html' !== $node_name && 'hr' !== $node_name && 'head' !== $node_name ) { 119 return $this->parse_node_h( $node, $attributes, $gutenberg_attribute ); 120 } 121 122 if ( 'img' === $node_name ) { 123 return $this->parse_node_img( $node, $attributes, $gutenberg_attribute ); 124 } 125 126 if ( 'ul' === $node_name ) { 127 return $this->parse_node_ul( $node, $attributes, $gutenberg_attribute ); 128 } 129 130 if ( 'ol' === $node_name ) { 131 return $this->parse_node_ol( $node, $attributes, $gutenberg_attribute ); 132 } 133 134 if ( 'hr' === $node_name ) { 135 return $this->parse_node_hr( $node, $attributes, $gutenberg_attribute ); 136 } 137 138 if ( 'blockquote' === $node_name ) { 139 return $this->parse_node_blockquote( $node, $attributes, $gutenberg_attribute ); 140 } 141 142 if ( 'pre' === $node_name ) { 143 return $this->parse_node_code( $node, $attributes, $gutenberg_attribute ); 144 } 145 146 if ( 'table' === $node_name ) { 147 return $this->parse_node_table( $node, $attributes, $gutenberg_attribute); 116 if (isset($all_attributes['gutenberg_block_specific'][$node_name])) { 117 $gutenberg_attribute = $all_attributes['gutenberg_block_specific'][$node_name]; 118 } 119 120 if ('p' === $node_name) { 121 return $this->parse_node_p($node, $attributes, $gutenberg_attribute); 122 } 123 124 if (0 === strpos($node_name, 'h') && 'html' !== $node_name && 'hr' !== $node_name && 'head' !== $node_name) { 125 return $this->parse_node_h($node, $attributes, $gutenberg_attribute); 126 } 127 128 if ('img' === $node_name) { 129 return $this->parse_node_img($node, $attributes, $gutenberg_attribute); 130 } 131 132 if ('ul' === $node_name) { 133 return $this->parse_node_ul($node, $attributes, $gutenberg_attribute); 134 } 135 136 if ('ol' === $node_name) { 137 return $this->parse_node_ol($node, $attributes, $gutenberg_attribute); 138 } 139 140 if ('hr' === $node_name) { 141 return $this->parse_node_hr($node, $attributes, $gutenberg_attribute); 142 } 143 144 if ('blockquote' === $node_name) { 145 return $this->parse_node_blockquote($node, $attributes, $gutenberg_attribute); 146 } 147 148 if ('pre' === $node_name) { 149 return $this->parse_node_code($node, $attributes, $gutenberg_attribute); 150 } 151 152 if ('table' === $node_name) { 153 return $this->parse_node_table($node, $attributes, $gutenberg_attribute); 154 } 155 156 if ('iframe' === $node_name) { 157 return $this->parse_node_iframe($node, $attributes, $gutenberg_attribute); 148 158 } 149 159 150 160 return ''; 151 161 } 162 152 163 153 164 /** … … 157 168 * @return array 158 169 */ 159 private function parse_node_attributes( $node ) { 170 private function parse_node_attributes($node) 171 { 160 172 161 173 $attributes_array = array(); … … 163 175 $attr_value = ''; 164 176 165 if ( $node->hasAttributes()) {177 if ($node->hasAttributes()) { 166 178 167 179 // @codingStandardsIgnoreLine 168 180 $node_name = $node->nodeName; 169 181 170 foreach ( $node->attributes as $attr) {182 foreach ($node->attributes as $attr) { 171 183 172 184 // @codingStandardsIgnoreLine … … 175 187 $attr_value = $attr->nodeValue; 176 188 177 if ( 'contenteditable' === $attr_name || 'class' === $attr_name) {189 if ('contenteditable' === $attr_name || 'class' === $attr_name) { 178 190 continue; 179 191 } 180 192 181 $attributes_array[ $attr_name] = $attr_value;193 $attributes_array[$attr_name] = $attr_value; 182 194 } 183 195 184 if ( in_array( $node_name, array( 'h2', 'h3', 'h4', 'h5', 'h6', 'h7' ), true ) && isset( $attr_name ) && 'style' === $attr_name) {185 $this->parse_h_special_attributes( $attr_value, $attributes_array, $block_specific_attributes);196 if (in_array($node_name, array('h2', 'h3', 'h4', 'h5', 'h6', 'h7'), true) && isset($attr_name) && 'style' === $attr_name) { 197 $this->parse_h_special_attributes($attr_value, $attributes_array, $block_specific_attributes); 186 198 } 187 199 188 if ( isset( $attr_name ) && 'p' === $node_name && 'style' === $attr_name) {189 $this->parse_p_special_attributes( $attr_value, $attributes_array, $block_specific_attributes);200 if (isset($attr_name) && 'p' === $node_name && 'style' === $attr_name) { 201 $this->parse_p_special_attributes($attr_value, $attributes_array, $block_specific_attributes); 190 202 } 191 203 } … … 204 216 * @param array $block_specific_attributes - reference to final array of gutenberg attributes. 205 217 */ 206 private function parse_p_special_attributes( $styles_string, &$attributes_array, &$block_specific_attributes ) { 207 208 $styles = explode( ';', $styles_string ); 218 private function parse_p_special_attributes($styles_string, &$attributes_array, &$block_specific_attributes) 219 { 220 221 $styles = explode(';', $styles_string); 209 222 $styles_assoc = array(); 210 foreach ( $styles as $style) {211 $s = explode( ':', $style);212 $styles_assoc[ $s[0] ] = trim( $s[1]);213 } 214 215 if ( key_exists( 'text-align', $styles_assoc )) {223 foreach ($styles as $style) { 224 $s = explode(':', $style); 225 $styles_assoc[$s[0]] = trim($s[1]); 226 } 227 228 if (key_exists('text-align', $styles_assoc)) { 216 229 $block_specific_attributes['p'] = '{"align":"' . $styles_assoc['text-align'] . '"} '; 217 230 } … … 225 238 * @param array $block_specific_attributes - reference to final array of gutenberg attributes. 226 239 */ 227 private function parse_h_special_attributes( $styles_string, &$attributes_array, &$block_specific_attributes ) { 228 229 $styles = explode( ';', $styles_string ); 240 private function parse_h_special_attributes($styles_string, &$attributes_array, &$block_specific_attributes) 241 { 242 243 $styles = explode(';', $styles_string); 230 244 $styles_assoc = array(); 231 foreach ( $styles as $style) {232 $s = explode( ':', $style);233 $styles_assoc[ $s[0] ] = trim( $s[1]);234 } 235 236 if ( key_exists( 'text-align', $styles_assoc )) {237 238 $styles_assoc['text-align'] = str_replace( 'start', 'left', $styles_assoc['text-align']);239 $styles_assoc['text-align'] = str_replace( 'end', 'right', $styles_assoc['text-align']);245 foreach ($styles as $style) { 246 $s = explode(':', $style); 247 $styles_assoc[$s[0]] = trim($s[1]); 248 } 249 250 if (key_exists('text-align', $styles_assoc)) { 251 252 $styles_assoc['text-align'] = str_replace('start', 'left', $styles_assoc['text-align']); 253 $styles_assoc['text-align'] = str_replace('end', 'right', $styles_assoc['text-align']); 240 254 241 255 $block_specific_attributes['h'] = '"textAlign":"' . $styles_assoc['text-align'] . '"'; 242 256 243 if ( ! isset( $attributes_array['class'] )) {257 if (! isset($attributes_array['class'])) { 244 258 $attributes_array['class'] = ''; 245 259 } 246 260 247 261 $attributes_array['class'] .= ' has-text-align-' . $styles_assoc['text-align']; 248 unset( $attributes_array['style']);262 unset($attributes_array['style']); 249 263 } 250 264 } … … 258 272 * @return string 259 273 */ 260 private function parse_node_p( $node, $attributes, $gutenberg_attribute ) { 261 262 $attributes = $this->glue_attributes( $attributes ); 263 264 $node_content = $this->get_inner_html( $node ); 265 if ( '' === $node_content ) { 266 return ''; 267 } 268 269 $attributes = str_replace( 'has-text-align-start', '', $attributes ); 274 private function parse_node_p($node, $attributes, $gutenberg_attribute) 275 { 276 277 $attributes = $this->glue_attributes($attributes); 278 279 $node_content = $this->get_inner_html($node); 280 if ('' === $node_content) { 281 return ''; 282 } 283 284 $attributes = str_replace('has-text-align-start', '', $attributes); 270 285 271 286 $content = '<!-- wp:paragraph ' . $gutenberg_attribute . '-->' . PHP_EOL; … … 284 299 * @return string 285 300 */ 286 private function parse_node_h( $node, $attributes, $gutenberg_attribute ) { 287 288 $h1_text = $this->get_inner_html( $node ); 301 private function parse_node_h($node, $attributes, $gutenberg_attribute) 302 { 303 304 $h1_text = $this->get_inner_html($node); 289 305 290 306 // @codingStandardsIgnoreLine 291 307 $node_name = $node->nodeName; 292 308 293 if ( 'h1' === $node_name && wp_strip_all_tags( $h1_text ) === $this->title) {294 return ''; 295 } 296 297 $attributes = $this->glue_attributes( $attributes);298 299 $header_size = str_replace( 'h', '', $node_name);309 if ('h1' === $node_name && wp_strip_all_tags($h1_text) === $this->title) { 310 return ''; 311 } 312 313 $attributes = $this->glue_attributes($attributes); 314 315 $header_size = str_replace('h', '', $node_name); 300 316 301 317 $content = '<!-- wp:heading {"level":' . $header_size . '} -->' . PHP_EOL; … … 306 322 } 307 323 324 325 /** 326 * Parse <iframe> node 327 * @param DOMNode|DOMDocument|DOMElement $node - node to parse. 328 * @param array $attributes - HTML attributes for the node, example: class="" src="". 329 * @param string $gutenberg_attribute - Attributes for Gutenberg tag. 330 * @return string 331 */ 332 private function parse_node_iframe($node, $attributes, $gutenberg_attribute) 333 { 334 335 $url = ''; 336 337 if (isset($attributes['src']) && ! empty($attributes['src'])) { 338 $url = $attributes['src']; 339 } 340 341 342 $content = '<!-- wp:embed {"url":"' . $url . '","type":"rich","providerNameSlug":"embed-handler","responsive":true,"className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} -->' . PHP_EOL; 343 $content .= 344 '<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">' . PHP_EOL; 345 $content .= $url . PHP_EOL; 346 $content .= '</div></figure>' . PHP_EOL; 347 $content .= '<!-- /wp:embed -->' . PHP_EOL . PHP_EOL; 348 349 return $content; 350 } 351 308 352 /** 309 353 * Parses <img> node. … … 314 358 * @return string 315 359 */ 316 private function parse_node_img( $node, $attributes, $gutenberg_attribute ) { 360 private function parse_node_img($node, $attributes, $gutenberg_attribute) 361 { 317 362 318 363 $image_url = ''; 319 364 $image_alt = ''; 320 365 321 if ( isset( $attributes['src'] ) && ! empty( $attributes['src'] )) {366 if (isset($attributes['src']) && ! empty($attributes['src'])) { 322 367 $image_url = $attributes['src']; 323 368 } 324 369 325 if ( isset( $attributes['alt'] ) && ! empty( $attributes['alt'] )) {370 if (isset($attributes['alt']) && ! empty($attributes['alt'])) { 326 371 $image_alt = $attributes['alt']; 327 372 } 328 373 329 $image_url = $this->download_img_to_media_library( $image_url, $image_alt);374 $image_url = $this->download_img_to_media_library($image_url, $image_alt); 330 375 $attributes['src'] = $image_url; 331 376 332 $attributes = $this->glue_attributes( $attributes);377 $attributes = $this->glue_attributes($attributes); 333 378 334 379 $content = '<!-- wp:image -->' . PHP_EOL; … … 349 394 * @return string 350 395 */ 351 private function parse_node_ul( $node, $attributes, $gutenberg_attribute ) { 352 353 $node_content = $this->get_inner_html( $node ); 354 if ( '' === $node_content ) { 396 private function parse_node_ul($node, $attributes, $gutenberg_attribute) 397 { 398 399 $node_content = $this->get_inner_html($node); 400 if ('' === $node_content) { 355 401 return ''; 356 402 } … … 372 418 * @param string $gutenberg_attribute - Attributes for Gutenberg tag. 373 419 */ 374 private function parse_node_table($node, $attributes, $gutenberg_attribute) { 375 376 $node_content = $this->get_inner_html( $node ); 377 if ( '' === $node_content ) { 420 private function parse_node_table($node, $attributes, $gutenberg_attribute) 421 { 422 423 $node_content = $this->get_inner_html($node); 424 if ('' === $node_content) { 378 425 return ''; 379 426 } … … 398 445 * @return string 399 446 */ 400 private function parse_node_ol( $node, $attributes, $gutenberg_attribute ) { 401 402 $node_content = $this->get_inner_html( $node ); 403 if ( '' === $node_content ) { 404 return ''; 405 } 406 407 $attributes = $this->glue_attributes( $attributes ); 447 private function parse_node_ol($node, $attributes, $gutenberg_attribute) 448 { 449 450 $node_content = $this->get_inner_html($node); 451 if ('' === $node_content) { 452 return ''; 453 } 454 455 $attributes = $this->glue_attributes($attributes); 408 456 409 457 $content = '<!-- wp:list {"ordered":true} -->' . PHP_EOL; … … 424 472 * @return string 425 473 */ 426 private function parse_node_hr( $node, $attributes, $gutenberg_attribute ) { 427 428 $attributes = $this->glue_attributes( $attributes ); 474 private function parse_node_hr($node, $attributes, $gutenberg_attribute) 475 { 476 477 $attributes = $this->glue_attributes($attributes); 429 478 430 479 $content = '<!-- wp:separator -->' . PHP_EOL; … … 443 492 * @return string 444 493 */ 445 private function parse_node_blockquote( $node, $attributes, $gutenberg_attribute ) { 446 447 $node_content = $this->get_inner_html( $node ); 448 if ( '' === $node_content ) { 494 private function parse_node_blockquote($node, $attributes, $gutenberg_attribute) 495 { 496 497 $node_content = $this->get_inner_html($node); 498 if ('' === $node_content) { 449 499 return ''; 450 500 } … … 465 515 * @return string 466 516 */ 467 private function parse_node_code( $node, $attributes, $gutenberg_attribute ) { 468 469 $node_content = $this->get_inner_html( $node ); 470 if ( '' === $node_content ) { 517 private function parse_node_code($node, $attributes, $gutenberg_attribute) 518 { 519 520 $node_content = $this->get_inner_html($node); 521 if ('' === $node_content) { 471 522 return ''; 472 523 } -
quickcreator/trunk/quickcreator.php
r3167407 r3173455 4 4 * Plugin URI: https://wordpress.org/plugins/quickcreator/ 5 5 * Description: Create post with Quickcreator in WordPress 6 * Version: 0. 0.96 * Version: 0.1.0 7 7 * Author: Quickcreator 8 8 * Author URI: https://quickcreator.io -
quickcreator/trunk/readme.txt
r3167407 r3173455 5 5 Requires PHP: 7.4 6 6 Tested up to: 6.6 7 Stable tag: 0. 0.97 Stable tag: 0.1.0 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset
for help on using the changeset viewer.