Plugin Directory

Changeset 3173455


Ignore:
Timestamp:
10/22/2024 08:33:54 AM (17 months ago)
Author:
quickcreator
Message:

update to 0.1.0

Location:
quickcreator
Files:
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • quickcreator/tags/0.1.0/includes/quickcreator/content-parsers/class-gutenberg-parser.php

    r3142565 r3173455  
    11<?php
     2
    23/**
    34 *  Parser that prepare data for Gutenberg
     
    1415 * Object that imports data from different sources into WordPress.
    1516 */
    16 class Gutenberg_Parser extends Content_Parser {
     17class Gutenberg_Parser extends Content_Parser
     18{
    1719
    1820    /**
     
    2224     * @return string
    2325     */
    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);
    2932
    3033        $doc = new DOMDocument();
     
    3336        $utf8_fix_suffix = '</body></html>';
    3437
    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);
    3639
    3740        $parsed_content = '';
    3841
    39         $this->parse_dom_node( $doc, $parsed_content );
     42        $this->parse_dom_node($doc, $parsed_content);
    4043
    4144        return $parsed_content;
     
    4952     * @return void
    5053     */
    51     private function parse_dom_node( $parent_node, &$content ) {
     54    private function parse_dom_node($parent_node, &$content)
     55    {
    5256        // @codingStandardsIgnoreLine
    53         foreach ( $parent_node->childNodes as $node ) {
     57        foreach ($parent_node->childNodes as $node) {
    5458
    5559            // @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);
    6266            }
    6367        }
     
    7074     * @return bool
    7175     */
    72     private function check_if_execute_recurrence( $node_type ) {
     76    private function check_if_execute_recurrence($node_type)
     77    {
    7378
    7479        $execute_for_child = true;
    7580
    76         if ( 'ul' === $node_type ) {
     81        if ('ul' === $node_type) {
    7782            $execute_for_child = false;
    7883        }
    7984
    80         if ( 'ol' === $node_type ) {
     85        if ('ol' === $node_type) {
    8186            $execute_for_child = false;
    8287        }
    8388
    84         if ( 'blockquote' === $node_type ) {
     89        if ('blockquote' === $node_type) {
    8590            $execute_for_child = false;
    8691        }
    8792
    88         if ( 'table' === $node_type ) {
     93        if ('table' === $node_type) {
    8994            $execute_for_child = false;
    9095        }
     
    99104     * @return string
    100105     */
    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);
    104110        $attributes     = $all_attributes['attributes'];
    105111
     
    108114
    109115        $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);
    148158        }
    149159
    150160        return '';
    151161    }
     162
    152163
    153164    /**
     
    157168     * @return array
    158169     */
    159     private function parse_node_attributes( $node ) {
     170    private function parse_node_attributes($node)
     171    {
    160172
    161173        $attributes_array          = array();
     
    163175        $attr_value                = '';
    164176
    165         if ( $node->hasAttributes() ) {
     177        if ($node->hasAttributes()) {
    166178
    167179            // @codingStandardsIgnoreLine
    168180            $node_name  = $node->nodeName;
    169181
    170             foreach ( $node->attributes as $attr ) {
     182            foreach ($node->attributes as $attr) {
    171183
    172184                // @codingStandardsIgnoreLine
     
    175187                $attr_value = $attr->nodeValue;
    176188
    177                 if ( 'contenteditable' === $attr_name || 'class' === $attr_name ) {
     189                if ('contenteditable' === $attr_name || 'class' === $attr_name) {
    178190                    continue;
    179191                }
    180192
    181                 $attributes_array[ $attr_name ] = $attr_value;
     193                $attributes_array[$attr_name] = $attr_value;
    182194            }
    183195
    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);
    186198            }
    187199
    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);
    190202            }
    191203        }
     
    204216     * @param array  $block_specific_attributes - reference to final array of gutenberg attributes.
    205217     */
    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);
    209222        $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)) {
    216229            $block_specific_attributes['p'] = '{"align":"' . $styles_assoc['text-align'] . '"} ';
    217230        }
     
    225238     * @param array  $block_specific_attributes - reference to final array of gutenberg attributes.
    226239     */
    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);
    230244        $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']);
    240254
    241255            $block_specific_attributes['h'] = '"textAlign":"' . $styles_assoc['text-align'] . '"';
    242256
    243             if ( ! isset( $attributes_array['class'] ) ) {
     257            if (! isset($attributes_array['class'])) {
    244258                $attributes_array['class'] = '';
    245259            }
    246260
    247261            $attributes_array['class'] .= ' has-text-align-' . $styles_assoc['text-align'];
    248             unset( $attributes_array['style'] );
     262            unset($attributes_array['style']);
    249263        }
    250264    }
     
    258272     * @return string
    259273     */
    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);
    270285
    271286        $content  = '<!-- wp:paragraph ' . $gutenberg_attribute . '-->' . PHP_EOL;
     
    284299     * @return string
    285300     */
    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);
    289305
    290306        // @codingStandardsIgnoreLine
    291307        $node_name = $node->nodeName;
    292308
    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);
    300316
    301317        $content  = '<!-- wp:heading {"level":' . $header_size . '} -->' . PHP_EOL;
     
    306322    }
    307323
     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
    308352    /**
    309353     * Parses <img> node.
     
    314358     * @return string
    315359     */
    316     private function parse_node_img( $node, $attributes, $gutenberg_attribute ) {
     360    private function parse_node_img($node, $attributes, $gutenberg_attribute)
     361    {
    317362
    318363        $image_url = '';
    319364        $image_alt = '';
    320365
    321         if ( isset( $attributes['src'] ) && ! empty( $attributes['src'] ) ) {
     366        if (isset($attributes['src']) && ! empty($attributes['src'])) {
    322367            $image_url = $attributes['src'];
    323368        }
    324369
    325         if ( isset( $attributes['alt'] ) && ! empty( $attributes['alt'] ) ) {
     370        if (isset($attributes['alt']) && ! empty($attributes['alt'])) {
    326371            $image_alt = $attributes['alt'];
    327372        }
    328373
    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);
    330375        $attributes['src'] = $image_url;
    331376
    332         $attributes = $this->glue_attributes( $attributes );
     377        $attributes = $this->glue_attributes($attributes);
    333378
    334379        $content  = '<!-- wp:image -->' . PHP_EOL;
     
    349394     * @return string
    350395     */
    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) {
    355401            return '';
    356402        }
     
    372418     * @param string                         $gutenberg_attribute - Attributes for Gutenberg tag.
    373419     */
    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) {
    378425            return '';
    379426        }
     
    398445     * @return string
    399446     */
    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);
    408456
    409457        $content  = '<!-- wp:list {"ordered":true} -->' . PHP_EOL;
     
    424472     * @return string
    425473     */
    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);
    429478
    430479        $content  = '<!-- wp:separator -->' . PHP_EOL;
     
    443492     * @return string
    444493     */
    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) {
    449499            return '';
    450500        }
     
    465515     * @return string
    466516     */
    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) {
    471522            return '';
    472523        }
  • quickcreator/tags/0.1.0/quickcreator.php

    r3167407 r3173455  
    44 * Plugin URI: https://wordpress.org/plugins/quickcreator/
    55 * Description: Create post with Quickcreator in WordPress
    6  * Version: 0.0.9
     6 * Version: 0.1.0
    77 * Author: Quickcreator
    88 * Author URI: https://quickcreator.io
  • quickcreator/tags/0.1.0/readme.txt

    r3167407 r3173455  
    55Requires PHP: 7.4
    66Tested up to: 6.6
    7 Stable tag: 0.0.9
     7Stable tag: 0.1.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
  • quickcreator/trunk/includes/quickcreator/content-parsers/class-gutenberg-parser.php

    r3142565 r3173455  
    11<?php
     2
    23/**
    34 *  Parser that prepare data for Gutenberg
     
    1415 * Object that imports data from different sources into WordPress.
    1516 */
    16 class Gutenberg_Parser extends Content_Parser {
     17class Gutenberg_Parser extends Content_Parser
     18{
    1719
    1820    /**
     
    2224     * @return string
    2325     */
    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);
    2932
    3033        $doc = new DOMDocument();
     
    3336        $utf8_fix_suffix = '</body></html>';
    3437
    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);
    3639
    3740        $parsed_content = '';
    3841
    39         $this->parse_dom_node( $doc, $parsed_content );
     42        $this->parse_dom_node($doc, $parsed_content);
    4043
    4144        return $parsed_content;
     
    4952     * @return void
    5053     */
    51     private function parse_dom_node( $parent_node, &$content ) {
     54    private function parse_dom_node($parent_node, &$content)
     55    {
    5256        // @codingStandardsIgnoreLine
    53         foreach ( $parent_node->childNodes as $node ) {
     57        foreach ($parent_node->childNodes as $node) {
    5458
    5559            // @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);
    6266            }
    6367        }
     
    7074     * @return bool
    7175     */
    72     private function check_if_execute_recurrence( $node_type ) {
     76    private function check_if_execute_recurrence($node_type)
     77    {
    7378
    7479        $execute_for_child = true;
    7580
    76         if ( 'ul' === $node_type ) {
     81        if ('ul' === $node_type) {
    7782            $execute_for_child = false;
    7883        }
    7984
    80         if ( 'ol' === $node_type ) {
     85        if ('ol' === $node_type) {
    8186            $execute_for_child = false;
    8287        }
    8388
    84         if ( 'blockquote' === $node_type ) {
     89        if ('blockquote' === $node_type) {
    8590            $execute_for_child = false;
    8691        }
    8792
    88         if ( 'table' === $node_type ) {
     93        if ('table' === $node_type) {
    8994            $execute_for_child = false;
    9095        }
     
    99104     * @return string
    100105     */
    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);
    104110        $attributes     = $all_attributes['attributes'];
    105111
     
    108114
    109115        $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);
    148158        }
    149159
    150160        return '';
    151161    }
     162
    152163
    153164    /**
     
    157168     * @return array
    158169     */
    159     private function parse_node_attributes( $node ) {
     170    private function parse_node_attributes($node)
     171    {
    160172
    161173        $attributes_array          = array();
     
    163175        $attr_value                = '';
    164176
    165         if ( $node->hasAttributes() ) {
     177        if ($node->hasAttributes()) {
    166178
    167179            // @codingStandardsIgnoreLine
    168180            $node_name  = $node->nodeName;
    169181
    170             foreach ( $node->attributes as $attr ) {
     182            foreach ($node->attributes as $attr) {
    171183
    172184                // @codingStandardsIgnoreLine
     
    175187                $attr_value = $attr->nodeValue;
    176188
    177                 if ( 'contenteditable' === $attr_name || 'class' === $attr_name ) {
     189                if ('contenteditable' === $attr_name || 'class' === $attr_name) {
    178190                    continue;
    179191                }
    180192
    181                 $attributes_array[ $attr_name ] = $attr_value;
     193                $attributes_array[$attr_name] = $attr_value;
    182194            }
    183195
    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);
    186198            }
    187199
    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);
    190202            }
    191203        }
     
    204216     * @param array  $block_specific_attributes - reference to final array of gutenberg attributes.
    205217     */
    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);
    209222        $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)) {
    216229            $block_specific_attributes['p'] = '{"align":"' . $styles_assoc['text-align'] . '"} ';
    217230        }
     
    225238     * @param array  $block_specific_attributes - reference to final array of gutenberg attributes.
    226239     */
    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);
    230244        $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']);
    240254
    241255            $block_specific_attributes['h'] = '"textAlign":"' . $styles_assoc['text-align'] . '"';
    242256
    243             if ( ! isset( $attributes_array['class'] ) ) {
     257            if (! isset($attributes_array['class'])) {
    244258                $attributes_array['class'] = '';
    245259            }
    246260
    247261            $attributes_array['class'] .= ' has-text-align-' . $styles_assoc['text-align'];
    248             unset( $attributes_array['style'] );
     262            unset($attributes_array['style']);
    249263        }
    250264    }
     
    258272     * @return string
    259273     */
    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);
    270285
    271286        $content  = '<!-- wp:paragraph ' . $gutenberg_attribute . '-->' . PHP_EOL;
     
    284299     * @return string
    285300     */
    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);
    289305
    290306        // @codingStandardsIgnoreLine
    291307        $node_name = $node->nodeName;
    292308
    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);
    300316
    301317        $content  = '<!-- wp:heading {"level":' . $header_size . '} -->' . PHP_EOL;
     
    306322    }
    307323
     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
    308352    /**
    309353     * Parses <img> node.
     
    314358     * @return string
    315359     */
    316     private function parse_node_img( $node, $attributes, $gutenberg_attribute ) {
     360    private function parse_node_img($node, $attributes, $gutenberg_attribute)
     361    {
    317362
    318363        $image_url = '';
    319364        $image_alt = '';
    320365
    321         if ( isset( $attributes['src'] ) && ! empty( $attributes['src'] ) ) {
     366        if (isset($attributes['src']) && ! empty($attributes['src'])) {
    322367            $image_url = $attributes['src'];
    323368        }
    324369
    325         if ( isset( $attributes['alt'] ) && ! empty( $attributes['alt'] ) ) {
     370        if (isset($attributes['alt']) && ! empty($attributes['alt'])) {
    326371            $image_alt = $attributes['alt'];
    327372        }
    328373
    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);
    330375        $attributes['src'] = $image_url;
    331376
    332         $attributes = $this->glue_attributes( $attributes );
     377        $attributes = $this->glue_attributes($attributes);
    333378
    334379        $content  = '<!-- wp:image -->' . PHP_EOL;
     
    349394     * @return string
    350395     */
    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) {
    355401            return '';
    356402        }
     
    372418     * @param string                         $gutenberg_attribute - Attributes for Gutenberg tag.
    373419     */
    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) {
    378425            return '';
    379426        }
     
    398445     * @return string
    399446     */
    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);
    408456
    409457        $content  = '<!-- wp:list {"ordered":true} -->' . PHP_EOL;
     
    424472     * @return string
    425473     */
    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);
    429478
    430479        $content  = '<!-- wp:separator -->' . PHP_EOL;
     
    443492     * @return string
    444493     */
    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) {
    449499            return '';
    450500        }
     
    465515     * @return string
    466516     */
    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) {
    471522            return '';
    472523        }
  • quickcreator/trunk/quickcreator.php

    r3167407 r3173455  
    44 * Plugin URI: https://wordpress.org/plugins/quickcreator/
    55 * Description: Create post with Quickcreator in WordPress
    6  * Version: 0.0.9
     6 * Version: 0.1.0
    77 * Author: Quickcreator
    88 * Author URI: https://quickcreator.io
  • quickcreator/trunk/readme.txt

    r3167407 r3173455  
    55Requires PHP: 7.4
    66Tested up to: 6.6
    7 Stable tag: 0.0.9
     7Stable tag: 0.1.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset for help on using the changeset viewer.