| 1 |
<?php |
| 2 |
/** |
| 3 |
* Actions functions |
| 4 |
* |
| 5 |
* @package Gutenify |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Gutenify_Actions |
| 14 |
*/ |
| 15 |
class Gutenify_Actions { |
| 16 |
|
| 17 |
/** |
| 18 |
* Gutenify_Actions constructor. |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
add_action( 'use_block_editor_for_post_type', array( $this, 'disable_gutenberg' ), 10, 2 ); |
| 22 |
add_filter( 'gutenify_skip_gutenburg_post_type', array( $this, 'skip_gutenburg_post_type' ) ); |
| 23 |
|
| 24 |
add_action( |
| 25 |
'save_post', |
| 26 |
function( $post_id, $post ) { |
| 27 |
// If this is just a revision, don't send the email. |
| 28 |
if ( wp_is_post_revision( $post_id ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$data = json_decode( file_get_contents( 'php://input' ), true ); |
| 33 |
// error_log( print_r( $data, true ) ); |
| 34 |
if ( ! empty( $data['meta']['gutenify_custom_css'] ) ) { |
| 35 |
$name = 'post-' . $post_id; |
| 36 |
if ( 'wp_template_parts' === $post->post_type ) { |
| 37 |
$name = 'part-' . $post_id; |
| 38 |
} |
| 39 |
if ( $this->create_styles_folder() ) { |
| 40 |
$this->create_styles_file( $name, gutenify_minimize_css_simple( $data['meta']['gutenify_custom_css'] ) ); |
| 41 |
} |
| 42 |
} |
| 43 |
}, |
| 44 |
10, |
| 45 |
2 |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Disable gutenber on post type. |
| 51 |
* |
| 52 |
* @param boolean $is_enabled If editor is enabled. |
| 53 |
* @param string $post_type Post type name. |
| 54 |
* @return boolean |
| 55 |
*/ |
| 56 |
public function disable_gutenberg( $is_enabled, $post_type ) { |
| 57 |
$skip_gutenburg = apply_filters( 'gutenify_skip_gutenburg_post_type', array() ); |
| 58 |
if ( ! in_array( $post_type, $skip_gutenburg, true ) ) { |
| 59 |
$settings = gutenify_settings(); |
| 60 |
$active_post_types = ! empty( $settings['active_post_types'] ) ? $settings['active_post_types'] : array(); |
| 61 |
if ( ! in_array( $post_type, $active_post_types, true ) ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
} |
| 65 |
return $is_enabled; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Post types to skip the options. |
| 70 |
* |
| 71 |
* @param array $post_types Post types. |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public function skip_gutenburg_post_type( $post_types ) { |
| 75 |
$post_types = array_merge( $post_types, array( 'attachment', 'wp_template', 'wp_block', 'gutenify_template' ) ); |
| 76 |
return $post_types; |
| 77 |
} |
| 78 |
|
| 79 |
public function create_styles_folder() { |
| 80 |
$uploads_dir = trailingslashit( wp_upload_dir()['basedir'] ) . 'gutenify/styles'; |
| 81 |
if ( ! file_exists( $uploads_dir ) ) { |
| 82 |
wp_mkdir_p( $uploads_dir ); |
| 83 |
} |
| 84 |
return true; |
| 85 |
// $fileLocation = $uploads_dir . '/myfile.txt'; |
| 86 |
// $file = fopen( $fileLocation, 'w' ); |
| 87 |
// $content = 'Your text here'; |
| 88 |
// fwrite( $file, $content ); |
| 89 |
// fclose( $file ); |
| 90 |
} |
| 91 |
|
| 92 |
public function create_styles_file( $name, $content ) { |
| 93 |
$uploads_dir = trailingslashit( wp_upload_dir()['basedir'] ) . 'gutenify/styles'; |
| 94 |
if ( file_exists( $uploads_dir ) ) { |
| 95 |
$fileLocation = $uploads_dir . '/style-' . $name . '.css'; |
| 96 |
$file = fopen( $fileLocation, 'w' ); |
| 97 |
fwrite( $file, $content ); |
| 98 |
fclose( $file ); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
new Gutenify_Actions(); |
| 103 |
|