Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Rob Sa
Added: May 31, 2021 8:36 AM
Modified: May 31, 2021 1:17 PM
Views: 197
Tags: wordpress
  1. <?php
  2.  
  3. require_once($_SERVER['DOCUMENT_ROOT'] . '/system/bootstraps/admin.inc.php');
  4. function get_html_split_regex() {
  5.     static $regex;
  6.  
  7.     if ( ! isset( $regex ) ) {
  8.         // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
  9.         $comments =
  10.             '!'             // Start of comment, after the <.
  11.             . '(?:'         // Unroll the loop: Consume everything until --> is found.
  12.             .     '-(?!->)' // Dash not followed by end of comment.
  13.             .     '[^\-]*+' // Consume non-dashes.
  14.             . ')*+'         // Loop possessively.
  15.             . '(?:-->)?';   // End of comment. If not found, match all input.
  16.  
  17.         $cdata =
  18.             '!\[CDATA\['    // Start of comment, after the <.
  19.             . '[^\]]*+'     // Consume non-].
  20.             . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
  21.             .     '](?!]>)' // One ] not followed by end of comment.
  22.             .     '[^\]]*+' // Consume non-].
  23.             . ')*+'         // Loop possessively.
  24.             . '(?:]]>)?';   // End of comment. If not found, match all input.
  25.  
  26.         $escaped =
  27.             '(?='             // Is the element escaped?
  28.             .    '!--'
  29.             . '|'
  30.             .    '!\[CDATA\['
  31.             . ')'
  32.             . '(?(?=!-)'      // If yes, which type?
  33.             .     $comments
  34.             . '|'
  35.             .     $cdata
  36.             . ')';
  37.  
  38.         $regex =
  39.             '/('                // Capture the entire match.
  40.             .     '<'           // Find start of element.
  41.             .     '(?'          // Conditional expression follows.
  42.             .         $escaped  // Find end of escaped element.
  43.             .     '|'           // ...else...
  44.             .         '[^>]*>?' // Find end of normal element.
  45.             .     ')'
  46.             . ')/';
  47.         // phpcs:enable
  48.     }
  49.  
  50.     return $regex;
  51. }
  52. function wp_html_split( $input ) {
  53.     return preg_split( get_html_split_regex(), $input, -1, PREG_SPLIT_DELIM_CAPTURE );
  54. }
  55. function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
  56.     // Find all elements.
  57.     $textarr = wp_html_split( $haystack );
  58.     $changed = false;
  59.  
  60.     // Optimize when searching for one item.
  61.     if ( 1 === count( $replace_pairs ) ) {
  62.         // Extract $needle and $replace.
  63.         foreach ( $replace_pairs as $needle => $replace ) {
  64.         }
  65.  
  66.         // Loop through delimiters (elements) only.
  67.         for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
  68.             if ( false !== strpos( $textarr[ $i ], $needle ) ) {
  69.                 $textarr[ $i ] = str_replace( $needle, $replace, $textarr[ $i ] );
  70.                 $changed       = true;
  71.             }
  72.         }
  73.     } else {
  74.         // Extract all $needles.
  75.         $needles = array_keys( $replace_pairs );
  76.  
  77.         // Loop through delimiters (elements) only.
  78.         for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
  79.             foreach ( $needles as $needle ) {
  80.                 if ( false !== strpos( $textarr[ $i ], $needle ) ) {
  81.                     $textarr[ $i ] = strtr( $textarr[ $i ], $replace_pairs );
  82.                     $changed       = true;
  83.                     // After one strtr() break out of the foreach loop and look at next element.
  84.                     break;
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.     if ( $changed ) {
  91.         $haystack = implode( $textarr );
  92.     }
  93.  
  94.     return $haystack;
  95. }
  96.  
  97. /**
  98.  * Replaces double line-breaks with paragraph elements.
  99.  *
  100.  * A group of regex replaces used to identify text formatted with newlines and
  101.  * replace double line-breaks with HTML paragraph tags. The remaining
  102.  * line-breaks after conversion become <<br />> tags, unless $br is set to '0'
  103.  * or 'false'.
  104.  *
  105.  * @param string $pee The text which has to be formatted.
  106.  * @param bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
  107.  * @return string Text which has been converted into correct paragraph tags.
  108.  */
  109. function wpautop( $pee, $br = true ) {
  110.     $pre_tags = array();
  111.  
  112.     if ( trim( $pee ) === '' ) {
  113.         return '';
  114.     }
  115.  
  116.     // Just to make things a little easier, pad the end.
  117.     $pee = $pee . "\n";
  118.  
  119.     /*
  120.      * Pre tags shouldn't be touched by autop.
  121.      * Replace pre tags with placeholders and bring them back after autop.
  122.      */
  123.     if ( strpos( $pee, '<pre' ) !== false ) {
  124.         $pee_parts = explode( '</pre>', $pee );
  125.         $last_pee  = array_pop( $pee_parts );
  126.         $pee       = '';
  127.         $i         = 0;
  128.  
  129.         foreach ( $pee_parts as $pee_part ) {
  130.             $start = strpos( $pee_part, '<pre' );
  131.  
  132.             // Malformed HTML?
  133.             if ( false === $start ) {
  134.                 $pee .= $pee_part;
  135.                 continue;
  136.             }
  137.  
  138.             $name              = "<pre wp-pre-tag-$i></pre>";
  139.             $pre_tags[ $name ] = substr( $pee_part, $start ) . '</pre>';
  140.  
  141.             $pee .= substr( $pee_part, 0, $start ) . $name;
  142.             $i++;
  143.         }
  144.  
  145.         $pee .= $last_pee;
  146.     }
  147.     // Change multiple <br>'s into two line breaks, which will turn into paragraphs.
  148.     $pee = preg_replace( '|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee );
  149.  
  150.     $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
  151.  
  152.     // Add a double line break above block-level opening tags.
  153.     $pee = preg_replace( '!(<' . $allblocks . '[\s/>])!', "\n\n$1", $pee );
  154.  
  155.     // Add a double line break below block-level closing tags.
  156.     $pee = preg_replace( '!(</' . $allblocks . '>)!', "$1\n\n", $pee );
  157.  
  158.     // Add a double line break after hr tags, which are self closing.
  159.     $pee = preg_replace( '!(<hr\s*?/?>)!', "$1\n\n", $pee );
  160.  
  161.     // Standardize newline characters to "\n".
  162.     $pee = str_replace( array( "\r\n", "\r" ), "\n", $pee );
  163.  
  164.     // Find newlines in all elements and add placeholders.
  165.     $pee = wp_replace_in_html_tags( $pee, array( "\n" => ' <!-- wpnl --> ' ) );
  166.  
  167.     // Collapse line breaks before and after <option> elements so they don't get autop'd.
  168.     if ( strpos( $pee, '<option' ) !== false ) {
  169.         $pee = preg_replace( '|\s*<option|', '<option', $pee );
  170.         $pee = preg_replace( '|</option>\s*|', '</option>', $pee );
  171.     }
  172.  
  173.     /*
  174.      * Collapse line breaks inside <object> elements, before <param> and <embed> elements
  175.      * so they don't get autop'd.
  176.      */
  177.     if ( strpos( $pee, '</object>' ) !== false ) {
  178.         $pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee );
  179.         $pee = preg_replace( '|\s*</object>|', '</object>', $pee );
  180.         $pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee );
  181.     }
  182.  
  183.     /*
  184.      * Collapse line breaks inside <audio> and <video> elements,
  185.      * before and after <source> and <track> elements.
  186.      */
  187.     if ( strpos( $pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) {
  188.         $pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee );
  189.         $pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee );
  190.         $pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee );
  191.     }
  192.  
  193.     // Collapse line breaks before and after <figcaption> elements.
  194.     if ( strpos( $pee, '<figcaption' ) !== false ) {
  195.         $pee = preg_replace( '|\s*(<figcaption[^>]*>)|', '$1', $pee );
  196.         $pee = preg_replace( '|</figcaption>\s*|', '</figcaption>', $pee );
  197.     }
  198.  
  199.     // Remove more than two contiguous line breaks.
  200.     $pee = preg_replace( "/\n\n+/", "\n\n", $pee );
  201.  
  202.     // Split up the contents into an array of strings, separated by double line breaks.
  203.     $pees = preg_split( '/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY );
  204.  
  205.     // Reset $pee prior to rebuilding.
  206.     $pee = '';
  207.  
  208.     // Rebuild the content as a string, wrapping every bit with a <p>.
  209.     foreach ( $pees as $tinkle ) {
  210.         $pee .= '<p>' . trim( $tinkle, "\n" ) . "</p>\n";
  211.     }
  212.  
  213.     // Under certain strange conditions it could create a P of entirely whitespace.
  214.     $pee = preg_replace( '|<p>\s*</p>|', '', $pee );
  215.  
  216.     // Add a closing <p> inside <div>, <address>, or <form> tag if missing.
  217.     $pee = preg_replace( '!<p>([^<]+)</(div|address|form)>!', '<p>$1</p></$2>', $pee );
  218.  
  219.     // If an opening or closing block element tag is wrapped in a <p>, unwrap it.
  220.     $pee = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $pee );
  221.  
  222.     // In some cases <li> may get wrapped in <p>, fix them.
  223.     $pee = preg_replace( '|<p>(<li.+?)</p>|', '$1', $pee );
  224.  
  225.     // If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
  226.     $pee = preg_replace( '|<p><blockquote([^>]*)>|i', '<blockquote$1><p>', $pee );
  227.     $pee = str_replace( '</blockquote></p>', '</p></blockquote>', $pee );
  228.  
  229.     // If an opening or closing block element tag is preceded by an opening <p> tag, remove it.
  230.     $pee = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)!', '$1', $pee );
  231.  
  232.     // If an opening or closing block element tag is followed by a closing <p> tag, remove it.
  233.     $pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $pee );
  234.  
  235.     // Optionally insert line breaks.
  236.     if ( $br ) {
  237.         // Replace newlines that shouldn't be touched with a placeholder.
  238.         $pee = preg_replace_callback( '/<(script|style|svg).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee );
  239.  
  240.         // Normalize <br>
  241.         $pee = str_replace( array( '<br>', '<br/>' ), '<br />', $pee );
  242.  
  243.         // Replace any new line characters that aren't preceded by a <br /> with a <br />.
  244.         $pee = preg_replace( '|(?<!<br />)\s*\n|', "<br />\n", $pee );
  245.  
  246.         // Replace newline placeholders with newlines.
  247.         $pee = str_replace( '<WPPreserveNewline />', "\n", $pee );
  248.     }
  249.  
  250.     // If a <br /> tag is after an opening or closing block tag, remove it.
  251.     $pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*<br />!', '$1', $pee );
  252.  
  253.     // If a <br /> tag is before a subset of opening or closing block tags, remove it.
  254.     $pee = preg_replace( '!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee );
  255.     $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
  256.  
  257.     // Replace placeholder <pre> tags with their original content.
  258.     if ( ! empty( $pre_tags ) ) {
  259.         $pee = str_replace( array_keys( $pre_tags ), array_values( $pre_tags ), $pee );
  260.     }
  261.  
  262.     // Restore newlines in all elements.
  263.     if ( false !== strpos( $pee, '<!-- wpnl -->' ) ) {
  264.         $pee = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $pee );
  265.     }
  266.  
  267.     return $pee;
  268. }
  269.  
  270. $sql = "
  271.    SELECT
  272.        *
  273.    FROM
  274.        pages
  275.    where id between 13 and 77
  276.        
  277. ";
  278.  
  279. $rs = $db->query($sql);
  280.  
  281. while($data = $db->fetch_assoc($rs)) {
  282.  
  283. //    $data['content'] = preg_replace(
  284. //        '/\[caption([^\]]+)align="([^"]+)"\s+width="(\d+)"\](\s*\<img[^>]+>)\s*(.*?)\s*\[\/caption\]/i',
  285. //        '<div\1style="width: \3px" class="wp-caption \2">\4<p class="caption">\5</p></div>',
  286. //        $data['content']);
  287.  
  288.  
  289.     $sql = sprintf("
  290.    update pages
  291.    set content = '%s'
  292.    where id = '%s'
  293. ", $db->escape_string(wpautop($data['content'])),
  294.         $data['id']
  295.     );
  296.  
  297.     echo $data['id'];
  298.     $db->query($sql);
  299.  
  300.     /*
  301.     $doc = new DOMDocument();
  302.     $doc->loadHTML($data['content']);
  303.     $xml = simplexml_import_dom($doc);
  304.     $images = $xml->xpath('//img');
  305.     foreach ($images as $img) {
  306.         echo $img['src'] . "<br />";
  307.     }*/
  308. }
  309.  
  310.  
  311.  
  312.  
  313. <?php
  314.  
  315. require_once($_SERVER['DOCUMENT_ROOT'] . '/system/bootstraps/admin.inc.php');
  316. ini_set('max_execution_time', 30 * 60); // increase maximum execution time to 30 minutes - some of the exports are intensive!
  317. ini_set('memory_limit', '8192M');
  318.  
  319.  
  320. $sql = "
  321.    SELECT
  322.        *
  323.    FROM
  324.        pages
  325.    where hero_image_file <> ''
  326.        
  327. ";
  328.  
  329. $rs = $db->query($sql);
  330.  
  331. while($data = $db->fetch_assoc($rs)) {
  332.  
  333.     // create thumbnail
  334.     //$cms->createImagesFromFile("page_hero", "https://dev.electriccarlease.co.uk" . $data['hero_image_file'], $data['id']);
  335.  
  336.     $cms->convertToWebp(PAGE_HERO_UPLOAD_FOLDER . "n_" . $data['id'] . ".jpg");
  337.  
  338.     $db->query($sql);
  339. }