Advertisement
newsprince

Cssgenerator

Feb 15th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.24 KB | None | 0 0
  1. <?php if ( ! defined( 'WOODMART_THEME_DIR' ) ) {
  2.     exit( 'No direct script access allowed' );}
  3.  
  4. /**
  5.  * Generate css file
  6.  */
  7.  
  8. class WOODMART_Cssgenerator {
  9.  
  10.     private $_generator_url = 'https://woodmart.xtemos.com/';
  11.  
  12.     private $_options = array();
  13.  
  14.     function __construct() {
  15.         $this->_notices = WOODMART_Registry()->notices;
  16.  
  17.         // add_action('init', array($this, 'init'));
  18.         $this->_options = woodmart_get_config( 'css-parts' );
  19.  
  20.     }
  21.  
  22.     private function _get_options_from_section( $section ) {
  23.         return array_filter(
  24.             $this->_options,
  25.             function( $el ) use ( $section ) {
  26.                 return $el['section'] == $section;
  27.             }
  28.         );
  29.     }
  30.  
  31.     public function init() {
  32.  
  33.     }
  34.  
  35.     private function _checked( $opt ) {
  36.  
  37.         $checked = $opt['checked'];
  38.  
  39.         if ( isset( $_POST['generate-css'] ) ) {
  40.             $checked = isset( $_POST[ $opt['id'] ] ) && $_POST[ $opt['id'] ];
  41.         }
  42.  
  43.         $css_data = $this->_get_data();
  44.  
  45.         if ( ! empty( $css_data ) && is_array( $css_data ) ) {
  46.             $checked = isset( $css_data[ $opt['id'] ] ) && $css_data[ $opt['id'] ];
  47.         }
  48.  
  49.         checked( $checked );
  50.     }
  51.  
  52.     private function _render_option( $opt ) {
  53.         ?>
  54.             <div class="css-checkbox" data-parent="<?php echo ( isset( $opt['parent'] ) ) ? $opt['parent'] : 'none'; ?>">
  55.                 <input type="checkbox" id="<?php echo esc_attr( $opt['id'] ); ?>" name="<?php echo esc_attr( $opt['id'] ); ?>" <?php $this->_checked( $opt ); ?> <?php disabled( isset( $opt['disabled'] ) && $opt['disabled'], true ); ?> value="true">
  56.                 <label for="<?php echo esc_attr( $opt['id'] ); ?>"><?php echo esc_html( $opt['title'] ); ?>
  57.                 <?php if ( isset( $opt['image'] ) || isset( $opt['description'] ) ) : ?>
  58.                     <div class="css-tooltip">
  59.                         <?php if ( isset( $opt['image'] ) ) : ?>
  60.                             <div class="css-tooltip-image">
  61.                                 <img src="<?php echo esc_attr( $opt['image'] ); ?>">
  62.                             </div>
  63.                         <?php endif ?>
  64.                         <?php if ( isset( $opt['description'] ) ) : ?>
  65.                             <p class="css-description"><?php echo esc_html( $opt['description'] ); ?></p>
  66.                         <?php endif ?>
  67.                     </div>
  68.                 <?php endif; ?>
  69.                 </label>
  70.                 <?php $this->_render_children( $opt['id'] ); ?>
  71.             </div>
  72.         <?php
  73.     }
  74.  
  75.     private function _render_children( $id ) {
  76.         $children = $this->_get_children( $id );
  77.  
  78.         if ( empty( $children ) ) {
  79.             return;
  80.         }
  81.  
  82.         echo '<div class="css-checkbox-children">';
  83.  
  84.         foreach ( $children as $id => $option ) {
  85.             $this->_render_option( $option );
  86.         }
  87.  
  88.         echo '</div>';
  89.     }
  90.  
  91.     private function _get_children( $id ) {
  92.         return array_filter(
  93.             $this->_options,
  94.             function( $el ) use ( $id ) {
  95.                 return isset( $el['parent'] ) && $el['parent'] == $id;
  96.             }
  97.         );
  98.     }
  99.  
  100.     private function _render_section( $name ) {
  101.         foreach ( $this->_get_options_from_section( $name ) as $id => $option ) {
  102.             if ( ! isset( $option['parent'] ) ) {
  103.                 $this->_render_option( $option );
  104.             }
  105.         }
  106.     }
  107.  
  108.     public function form() {
  109.         $this->init();
  110.         $this->process_form();
  111.         $this->_notices->show_msgs();
  112.  
  113.         $file  = get_option( 'woodmart-generated-css-file' );
  114.         $theme = wp_get_theme( get_template() );
  115.         ?>
  116.  
  117.         <div class="xtemos-loader-wrapper">
  118.             <div class="xtemos-loader">
  119.                 <div class="xtemos-loader-el">
  120.                     <img src="<?php echo WOODMART_ASSETS . '/images/loader/loader-el-1.svg'; ?>">
  121.                 </div>
  122.                 <div class="xtemos-loader-el">
  123.                     <img src="<?php echo WOODMART_ASSETS . '/images/loader/loader-el-2.svg'; ?>">
  124.                 </div>
  125.             </div>
  126.             <p>Generating... It may take a few minutes...</p>
  127.         </div>
  128.  
  129.         <h3><?php esc_html_e( 'Custom CSS file generator', 'woodmart' ); ?></h3>
  130.         <p>
  131.             This interface gives you an ability to reduce CSS file size by reducing amount of code
  132.             loaded on each page. Just untick all features, options and plugins that you will not use
  133.             and this CSS code will not be loaded. You can make your CSS file up to 50% smaller and it
  134.             depends on a number of options and features you use.
  135.         </p>
  136.  
  137.         <form action="" method="post" class="woodmart-form woodmart-generator-form">
  138.             <div class="woodmart-row woodmart-three-columns">
  139.                 <div class="woodmart-column">
  140.                     <div class="woodmart-column-inner">
  141.                         <div class="css-options-box">
  142.                             <h4>Blog</h4>
  143.  
  144.                             <?php $this->_render_section( 'Blog' ); ?>
  145.                         </div>
  146.                         <div class="css-options-box">
  147.                             <h4>Portfolio</h4>
  148.  
  149.                             <?php $this->_render_section( 'Portfolio' ); ?>
  150.                         </div>
  151.                         <div class="css-options-box">
  152.                             <h4>Additional plugins</h4>
  153.  
  154.                             <?php $this->_render_section( 'Additional plugins' ); ?>
  155.                         </div>
  156.                         <div class="css-options-box">
  157.                             <h4>Extra features</h4>
  158.  
  159.                             <?php $this->_render_section( 'Extra features' ); ?>
  160.                         </div>
  161.                     </div>
  162.                 </div>
  163.                 <div class="woodmart-column">
  164.                     <div class="woodmart-column-inner">
  165.                         <div class="css-options-box">
  166.                             <h4>WooCommerce styles</h4>
  167.  
  168.                             <?php $this->_render_section( 'WooCommerce styles' ); ?>
  169.  
  170.                         </div>
  171.                     </div>
  172.                 </div>
  173.                 <div class="woodmart-column">
  174.                     <div class="woodmart-column-inner css-tooltip-right">
  175.                         <div class="css-options-box">
  176.                             <h4>WPBakery elements</h4>
  177.  
  178.                             <?php $this->_render_section( 'WPBakery elements' ); ?>
  179.                         </div>
  180.                         <div class="css-options-box">
  181.                             <h4>Extra configuration</h4>
  182.  
  183.                             <?php $this->_render_section( 'Extra configuration' ); ?>
  184.                         </div>
  185.                         <div class="css-options-box">
  186.                             <h4>Experimental Header Breakpoint Width</h4>
  187.                             <p>Enter value (in px) between 648 - 1919 or leave it as is.</p>
  188.                             <p>The mobile header would be applied from that value and bellow.</p>
  189.                             <input type="number" id="hbreakpoint" name="hbreakpoint" value="1024" min="648" max="1919">
  190.                         </div>
  191.                     </div>
  192.                 </div>
  193.             </div>
  194.  
  195.             <input type="hidden" name="css-data">
  196.  
  197.             <input class="button-primary" name="generate-css" type="submit" value="<?php esc_attr_e( 'Generate file', 'woodmart' ); ?>" />
  198.  
  199.         </form>
  200.  
  201.         <?php if ( $file['file'] ) : ?>
  202.  
  203.             <div class="css-file-information">
  204.  
  205.                 <h3>Custom CSS file is <span>generated</span></h3>
  206.  
  207.                 <?php
  208.                     $data = get_file_data( $file['file'], array( 'Version' => 'Version' ) );
  209.                 ?>
  210.  
  211.                 <table>
  212.                     <tr>
  213.                         <th>
  214.                             File:
  215.                         </th>
  216.                         <td>
  217.                             <a href="<?php echo esc_url( $file['url'] ); ?>" target="_blank"><?php echo esc_html( $file['name'] ); ?></a>
  218.                         </td>
  219.                     </tr>
  220.                     <tr>
  221.                         <th>
  222.                             CSS Version:
  223.                         </th>
  224.                         <td>
  225.                             <strong><?php echo ( isset( $data['Version'] ) ) ? $data['Version'] : 'unknown'; ?></strong>
  226.                         </td>
  227.                     </tr>
  228.                     <tr>
  229.                         <th>
  230.                             Theme version:
  231.                         </th>
  232.                         <td>
  233.                             <strong><?php echo esc_html( $theme->get( 'Version' ) ); ?></strong>
  234.                         </td>
  235.                     </tr>
  236.                 </table>
  237.                
  238.                 <div class="css-file-actions">
  239.                     <?php if ( version_compare( $data['Version'], $theme->get( 'Version' ), '<' ) ) : ?>
  240.                         <input class="button-primary css-update-button" name="deactivate-css" type="submit" value="<?php esc_attr_e( 'Update', 'woodmart' ); ?>" />
  241.                     <?php endif ?>
  242.                     <a href="<?php echo admin_url( 'admin.php?page=woodmart_dashboard&tab=css&deactivate-css=1' ); ?>" class="button-primary" name="deactivate-css" type="submit">
  243.                         <?php esc_attr_e( 'Delete', 'woodmart' ); ?>
  244.                     </a>
  245.                 </div>
  246.             </div>
  247.  
  248.         <?php endif; ?>
  249.  
  250.         <?php
  251.     }
  252.  
  253.     public function process_form() {
  254.  
  255.         if ( isset( $_GET['deactivate-css'] ) ) {
  256.             $file = get_option( 'woodmart-generated-css-file' );
  257.  
  258.             if ( $file['file'] ) {
  259.                 unlink( $file['file'] );
  260.             }
  261.  
  262.             delete_option( 'woodmart-generated-css-file' );
  263.             delete_option( 'woodmart-css-data' );
  264.  
  265.         }
  266.  
  267.         if ( ! isset( $_POST['generate-css'] ) || empty( $_POST['generate-css'] ) ) {
  268.             return;
  269.         }
  270.  
  271.         $data = $_POST['css-data'];
  272.  
  273.         if ( function_exists( 'woodmart_decompress' ) && ! $json = woodmart_decompress( $data ) ) {
  274.             $this->_notices->add_warning( 'Wrong data sent. Try to resend the form.' );
  275.             return;
  276.         }
  277.  
  278.         if ( ! $css_data = json_decode( $json, true ) ) {
  279.             $this->_notices->add_warning( 'Wrong JSON data format. Try to resend the form.' );
  280.             return;
  281.         }
  282.  
  283.         /* you can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */
  284.         $creds = request_filesystem_credentials( false, '', false, false, array_keys( $_POST ) );
  285.  
  286.         if ( ! $creds ) {
  287.             return;
  288.         }
  289.  
  290.         /* initialize the API */
  291.         if ( ! WP_Filesystem( $creds ) ) {
  292.             /* any problems and we exit */
  293.             $this->_notices->add_warning( 'Can\'t access your file system. The FTP access is wrong.' );
  294.             return false;
  295.         }
  296.  
  297.         global $wp_filesystem;
  298.  
  299.         $response = wp_remote_get( $this->_generator_url . '?generate_css=' . $data . '&theme_url=' . urlencode( get_template_directory_uri() ) . '/', array( 'timeout' => 30 ) );
  300.  
  301.         if ( isset( $_GET['xtemos_debug'] ) ) {
  302.             ar( $response );
  303.         }
  304.  
  305.         if ( ! is_array( $response ) ) {
  306.             $this->_notices->add_warning( 'Can\'t call xtemos server to generate the file.' );
  307.             return false;
  308.         }
  309.  
  310.         $header = $response['headers']; // array of http header lines
  311.         $css    = $response['body']; // use the content
  312.        
  313.         $breakpoint = sanitize_text_field($_POST["hbreakpoint"]);
  314.         $precss = str_replace ( "1024", "$breakpoint", $response['body'] );
  315.         $breakpoint++;
  316.         $css = str_replace ( "1025", "$breakpoint", $precss );
  317.  
  318.         $t         = time();
  319.         $file_name = 'style-' . $t . '.css';
  320.  
  321.         $uploads = wp_upload_dir();
  322.  
  323.         $file_path = $uploads['path'] . '/' . $file_name;
  324.         $file_url  = $uploads['url'] . '/' . $file_name;
  325.  
  326.         $res = $wp_filesystem->put_contents(
  327.             $file_path,
  328.             $css
  329.         );
  330.  
  331.         if ( $res ) {
  332.  
  333.             $upload = array(
  334.                 'name' => $file_name,
  335.                 'url'  => $file_url,
  336.                 'file' => $file_path,
  337.             );
  338.  
  339.             $file = get_option( 'woodmart-generated-css-file' );
  340.  
  341.             if ( $file['file'] ) {
  342.                 $wp_filesystem->delete( $file['file'] );
  343.             }
  344.  
  345.             update_option( 'woodmart-generated-css-file', $upload );
  346.             update_option( 'woodmart-css-data', $css_data );
  347.  
  348.             $this->_notices->add_success( 'New CSS file is generated and saved.' );
  349.  
  350.         } else {
  351.             $this->_notices->add_warning( 'Can\'t move file to uploads folder with wp_filesystem class.' );
  352.             return false;
  353.         }
  354.  
  355.     }
  356.  
  357.     private function _get_data() {
  358.         $css_data = get_option( 'woodmart-css-data', array() );
  359.  
  360.         return $css_data;
  361.     }
  362.  
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement