index.php
181 lines
| 1 | <?php |
| 2 | namespace KirkiComponentLib; |
| 3 | |
| 4 | use Kirki\HelperFunctions; |
| 5 | |
| 6 | define( 'KIRKI_COMPONENT_LIBRARY_APP_PREFIX', 'KirkiComponentLibrary' ); |
| 7 | define( 'KIRKI_COMPONENT_LIBRARY_ROOT_URL', plugin_dir_url( __FILE__ ) ); |
| 8 | define( 'KIRKI_COMPONENT_LIBRARY_ROOT_PATH', plugin_dir_path( __FILE__ ) ); |
| 9 | |
| 10 | require_once KIRKI_COMPONENT_LIBRARY_ROOT_PATH . '/controller/CompLibFormHandler.php'; |
| 11 | require_once KIRKI_COMPONENT_LIBRARY_ROOT_PATH . '/controller/ShowUserMetadata.php'; |
| 12 | require_once KIRKI_COMPONENT_LIBRARY_ROOT_PATH . '/controller/ElementGenerator.php'; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | class KirkiComponentLibrary { |
| 19 | |
| 20 | private $component_lib_forms = array(); |
| 21 | |
| 22 | public function __construct() { |
| 23 | $this->init(); |
| 24 | add_filter( 'kirki_element_generator_' . KIRKI_COMPONENT_LIBRARY_APP_PREFIX, array( $this, 'element_generator' ), 10, 2 ); |
| 25 | add_filter( 'kirki_external_collection_options', array( $this, 'modify_external_collection_options' ), 10, 2 ); |
| 26 | add_filter( 'kirki_collection_comments', array( $this, 'kirki_collection_comments' ), 10, 2 ); |
| 27 | |
| 28 | add_filter( 'kirki_dynamic_content', array( $this, 'kirki_dynamic_content' ), 10, 2 ); |
| 29 | new ShowUserMetadata(); |
| 30 | } |
| 31 | |
| 32 | public function init() { |
| 33 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 34 | $action = sanitize_text_field( isset( $_GET['action'] ) ? $_GET['action'] : null ); |
| 35 | if ( 'kirki' === $action ) { |
| 36 | $load_for = sanitize_text_field( isset( $_GET['load_for'] ) ? wp_unslash( $_GET['load_for'] ) : null ); |
| 37 | if ( 'kirki-iframe' !== $load_for ) { |
| 38 | add_action( 'wp_enqueue_scripts', array( $this, 'load_editor_assets' ), 1 ); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function load_editor_assets() { |
| 44 | wp_enqueue_script( KIRKI_COMPONENT_LIBRARY_APP_PREFIX . '-editor', KIRKI_COMPONENT_LIBRARY_ROOT_URL . 'assets/js/' . 'editor.min.js', array(), KIRKI_VERSION, array( 'in_footer' => true ) ); |
| 45 | wp_add_inline_script( |
| 46 | KIRKI_COMPONENT_LIBRARY_APP_PREFIX . '-editor', |
| 47 | 'const ' . KIRKI_COMPONENT_LIBRARY_APP_PREFIX . ' = ' . json_encode( |
| 48 | array( |
| 49 | 'base_url' => KIRKI_COMPONENT_LIBRARY_ROOT_URL, |
| 50 | ) |
| 51 | ), |
| 52 | 'before' |
| 53 | ); |
| 54 | add_action( |
| 55 | 'wp_enqueue_scripts', |
| 56 | function () { |
| 57 | global $kirki_editor_assets; |
| 58 | $kirki_editor_assets['scripts'][] = KIRKI_COMPONENT_LIBRARY_APP_PREFIX . '-editor'; |
| 59 | }, |
| 60 | 50 |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | public function add_component_library_script( $script_tags ) { |
| 65 | $value = $this->component_lib_forms; |
| 66 | $val = wp_json_encode( $value ); |
| 67 | $script = 'var ' . KIRKI_COMPONENT_LIBRARY_APP_PREFIX . ' = window.' . KIRKI_COMPONENT_LIBRARY_APP_PREFIX . " === undefined? {form: $val, root_url:'" . KIRKI_COMPONENT_LIBRARY_ROOT_URL . "'} : {..." . KIRKI_COMPONENT_LIBRARY_APP_PREFIX . ', form:{...(' . KIRKI_COMPONENT_LIBRARY_APP_PREFIX . ".form || {}), ...$val}};"; |
| 68 | |
| 69 | $script_tags .= "<script data='" . KIRKI_COMPONENT_LIBRARY_APP_PREFIX . "-elements-property-vars'>$script</script>"; |
| 70 | |
| 71 | return $script_tags; |
| 72 | } |
| 73 | |
| 74 | public function load_element_scripts_and_styles() { |
| 75 | add_filter('kirki_add_script_tags', array( $this, 'add_component_library_script' ) ); |
| 76 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 77 | $action = sanitize_text_field( isset( $_GET['action'] ) ? $_GET['action'] : null ); |
| 78 | if ( 'kirki' !== $action ) { |
| 79 | add_action( |
| 80 | 'wp_enqueue_scripts', |
| 81 | function () { |
| 82 | wp_enqueue_style( KIRKI_COMPONENT_LIBRARY_APP_PREFIX, KIRKI_COMPONENT_LIBRARY_ROOT_URL . 'assets/css/' . 'main.min.css', array(), KIRKI_VERSION ); |
| 83 | wp_enqueue_script( KIRKI_COMPONENT_LIBRARY_APP_PREFIX, KIRKI_COMPONENT_LIBRARY_ROOT_URL . 'assets/js/' . 'preview.min.js', array(), KIRKI_VERSION, array( 'in_footer' => true ) ); |
| 84 | } |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public function element_generator( $string, $props ) { |
| 90 | $this->load_element_scripts_and_styles(); |
| 91 | $props['component_lib_forms'] = $this->component_lib_forms; |
| 92 | $hide = false; |
| 93 | if ( |
| 94 | 'kirki-login-error' === $props['element']['name'] || |
| 95 | 'kirki-register-error' === $props['element']['name'] || |
| 96 | 'kirki-forgot-password-error' === $props['element']['name'] || |
| 97 | 'kirki-change-password-error' === $props['element']['name'] || |
| 98 | 'kirki-retrieve-username-error' === $props['element']['name'] |
| 99 | ) { |
| 100 | $hide = true; |
| 101 | } |
| 102 | $eg = new ElementGenerator( $props ); |
| 103 | $gen = $eg->generate_common_element( $hide ); |
| 104 | $this->component_lib_forms = $eg->component_lib_forms; |
| 105 | return $gen; |
| 106 | } |
| 107 | |
| 108 | public function modify_external_collection_options( $options, $args ) { |
| 109 | $comment_collection = array( |
| 110 | 'title' => 'Comments', |
| 111 | 'value' => 'comments', |
| 112 | 'inherit' => true, |
| 113 | 'pegination' => true, |
| 114 | 'default_select_type' => 'comment', |
| 115 | 'group' => array( |
| 116 | array( |
| 117 | 'title' => 'Post Comments', |
| 118 | 'value' => 'comment', |
| 119 | 'itemType' => 'comment', |
| 120 | ), |
| 121 | // TODO: need to get all comment type and add it as list here. |
| 122 | ), |
| 123 | ); |
| 124 | $options[] = $comment_collection; |
| 125 | return $options; |
| 126 | } |
| 127 | |
| 128 | public function kirki_collection_comments( $value, $args ) { |
| 129 | $all_comments = array(); |
| 130 | $c_args = array( 'post_id' => $args['post_parent'] ); |
| 131 | if ( isset( $args['parent_item_type'] ) && $args['parent_item_type'] === 'comment' ) { |
| 132 | $c_args['parent'] = $args['context']['comment_ID']; |
| 133 | } elseif ( isset( $args['context']['comment_ID'] ) ) { |
| 134 | $c_args['parent'] = $args['context']['comment_ID']; |
| 135 | } |
| 136 | |
| 137 | $c_args['current_page'] = 1; |
| 138 | $c_args['item_per_page'] = 100; |
| 139 | $ac = HelperFunctions::get_comments( $c_args ); |
| 140 | |
| 141 | foreach ( $ac['data'] as $comment ) { |
| 142 | $all_comments[] = array( |
| 143 | 'id' => $comment->comment_ID, |
| 144 | 'comment_post_ID' => $comment->comment_post_ID, |
| 145 | 'comment_author' => $comment->comment_author, |
| 146 | 'comment_author_email' => $comment->comment_author_email, |
| 147 | 'comment_author_url' => $comment->comment_author_url, |
| 148 | 'comment_date' => $comment->comment_date, |
| 149 | 'comment_date_gmt' => $comment->comment_date_gmt, |
| 150 | 'comment_content' => $comment->comment_content, |
| 151 | 'comment_approved' => $comment->comment_approved, |
| 152 | 'comment_agent' => $comment->comment_agent, |
| 153 | 'comment_type' => $comment->comment_type, |
| 154 | 'comment_parent' => $comment->comment_parent, |
| 155 | 'user_id' => $comment->user_id, |
| 156 | ); |
| 157 | } |
| 158 | return array( |
| 159 | 'data' => $all_comments, |
| 160 | 'pagination' => array(), |
| 161 | 'itemType' => 'comment', |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | public function kirki_dynamic_content( $value, $args ) { |
| 166 | if ( isset( $args['dynamicContent'] ) ) { |
| 167 | if ( $args['dynamicContent']['type'] === 'comment' ) { |
| 168 | if ( isset( $args['options']['comment'], $args['dynamicContent']['value'], $args['options']['comment'][ $args['dynamicContent']['value'] ] ) ) { |
| 169 | return $args['options']['comment'][ $args['dynamicContent']['value'] ]; |
| 170 | } elseif ( isset( $args['dynamicContent']['value'] ) ) { |
| 171 | return $args['dynamicContent']['value']; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | return $value; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | new KirkiComponentLibrary(); |
| 181 |