| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use WPCoder\Dashboard\DBManager; |
| 8 |
use WPCoder\Dashboard\FolderManager; |
| 9 |
use WPCoder\Optimization\HTMLMinifier; |
| 10 |
use WPCoder\Publisher\Conditions; |
| 11 |
use WPCoder\Publisher\EnqueueScript; |
| 12 |
use WPCoder\Publisher\EnqueueStyle; |
| 13 |
use WPCoder\Publisher\PHPIncludes; |
| 14 |
use WPCoder\Publisher\Singleton; |
| 15 |
|
| 16 |
class WOWP_Public { |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
add_shortcode( 'WP-Coder', [ $this, 'shortcode' ] ); |
| 20 |
add_shortcode( WPCoder::SHORTCODE, [ $this, 'shortcode' ] ); |
| 21 |
|
| 22 |
add_action( 'wp_footer', [ $this, 'print_footer' ], 50 ); |
| 23 |
|
| 24 |
new PHPIncludes; |
| 25 |
} |
| 26 |
|
| 27 |
public function shortcode( $atts, $shortcode_content = null ) { |
| 28 |
if ( ! empty( $atts['id'] ) ) { |
| 29 |
$result = DBManager::get_data_by_id( $atts['id'] ); |
| 30 |
} elseif ( ! empty( $atts['title'] ) ) { |
| 31 |
$result = DBManager::get_data_by_title( $atts['title'] ); |
| 32 |
} else { |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
if ( empty( $result ) ) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
$default_atts = [ 'id' => "", 'title' => '' ]; |
| 41 |
$add_atts = $this->get_shortcode_atts( $result ); |
| 42 |
$filtered_new_atts = array_diff_key( $add_atts, $default_atts ); |
| 43 |
$result_atts = array_merge( $default_atts, $filtered_new_atts ); |
| 44 |
|
| 45 |
$attrs = shortcode_atts( $result_atts, $atts, WPCoder::SHORTCODE ); |
| 46 |
|
| 47 |
if ( Conditions::init( $result ) === false ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
$wp_coder_content_out = $this->get_content( $result, $shortcode_content, $attrs ); |
| 53 |
|
| 54 |
$singleton = Singleton::getInstance(); |
| 55 |
$singleton->setValue( $result->id, $result ); |
| 56 |
|
| 57 |
EnqueueStyle::init( $result ); |
| 58 |
EnqueueScript::init( $result ); |
| 59 |
|
| 60 |
return $wp_coder_content_out; |
| 61 |
} |
| 62 |
|
| 63 |
private function get_shortcode_atts( $result ): array { |
| 64 |
$param = maybe_unserialize( $result->param ); |
| 65 |
if ( empty( $param['shortcode_attribute'] ) || ! is_array( $param['shortcode_attribute'] ) ) { |
| 66 |
return array(); |
| 67 |
} |
| 68 |
|
| 69 |
return array_fill_keys( $param['shortcode_attribute'], '' ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
public function print_footer() { |
| 74 |
$singleton = Singleton::getInstance(); |
| 75 |
$shortcodes = $singleton->getValue(); |
| 76 |
|
| 77 |
foreach ( $shortcodes as $id => $result ) { |
| 78 |
EnqueueStyle::inline( $result ); |
| 79 |
EnqueueScript::inline( $result ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
private function get_content( $result, $shortcode_content = null, $attrs = null ) { |
| 84 |
$shortcode_content = !empty($shortcode_content) ? do_shortcode( $shortcode_content ) : ''; |
| 85 |
$wp_coder_content = !empty($result->html_code) ? do_shortcode( $result->html_code ) : ''; |
| 86 |
|
| 87 |
if ( ! empty( $attrs ) ) { |
| 88 |
extract( $attrs, EXTR_PREFIX_SAME, "wpcp" ); |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! empty( $result->php_code ) ) { |
| 92 |
$path = FolderManager::code_path( $result ); |
| 93 |
include $path; |
| 94 |
} |
| 95 |
|
| 96 |
if ( preg_match_all( '/\{\{(.*?)\}\}/', $wp_coder_content, $matches ) && is_array( $matches[1] ) ) { |
| 97 |
foreach ( $matches[1] as $match ) { |
| 98 |
$wpcoder_variable = str_replace( '$', '', $match ); |
| 99 |
if ( isset( $$wpcoder_variable ) ) { |
| 100 |
$wp_coder_content = str_replace( '{{' . $match . '}}', $$wpcoder_variable, $wp_coder_content ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$param = ! empty( $result->param ) ? maybe_unserialize( $result->param ) : []; |
| 106 |
if ( ! empty( $param['minified_html'] ) ) { |
| 107 |
return HTMLMinifier::minify( $wp_coder_content ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
return $wp_coder_content; |
| 112 |
} |
| 113 |
|
| 114 |
} |