545 lines
23 KiB
PHP
545 lines
23 KiB
PHP
<?php
|
|
|
|
class SHEET_DATA_CHECKER_PRO {
|
|
|
|
/**
|
|
* A reference to an instance of this class.
|
|
*/
|
|
private static $instance;
|
|
|
|
/**
|
|
* Returns an instance of this class.
|
|
*/
|
|
public static function get_instance() {
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
/**
|
|
* Initializes the plugin by setting filters and administration functions.
|
|
*/
|
|
public function __construct() {
|
|
|
|
add_action( 'init', [$this, 'create_custom_post_type'] );
|
|
add_action( 'admin_enqueue_scripts', [$this, 'enqueue_bootstrap_admin'] );
|
|
|
|
include SHEET_CHECKER_PRO_PATH . 'includes/class-License.php';
|
|
$lis = new CHECKER_LICENSE();
|
|
|
|
if(true == $lis->the_lis()){
|
|
|
|
add_filter( 'manage_checker_posts_columns', [$this, 'filter_cpt_columns']);
|
|
add_action( 'manage_checker_posts_custom_column', [$this, 'action_custom_columns_content'], 10, 2 );
|
|
|
|
add_action( 'add_meta_boxes', [$this, 'add_checker_metabox'] );
|
|
add_action( 'save_post_checker', [$this, 'save_checker_metabox'] );
|
|
|
|
add_action( 'wp_ajax_load_repeater_field_card', [$this, 'load_repeater_field_card'] );
|
|
add_action( 'wp_ajax_load_output_setting', [$this, 'load_output_setting'] );
|
|
|
|
require 'class-Shortcode.php';
|
|
new CHECKER_SHORTCODE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function create_custom_post_type() {
|
|
|
|
$labels = array(
|
|
'name' => 'Checker',
|
|
'singular_name' => 'Checker',
|
|
'menu_name' => 'Checkers',
|
|
'add_new' => 'Add New',
|
|
'add_new_item' => 'Add New Checker',
|
|
'edit' => 'Edit',
|
|
'edit_item' => 'Edit Checker',
|
|
'new_item' => 'New Checker',
|
|
'view' => 'View',
|
|
'view_item' => 'View Checker',
|
|
'search_items' => 'Search Checkers',
|
|
'not_found' => 'No checkers found',
|
|
'not_found_in_trash' => 'No checkers found in trash',
|
|
'parent' => 'Parent Checker'
|
|
);
|
|
|
|
$args = array(
|
|
'label' => 'Checkers',
|
|
'description' => 'Checkers for your sheet data',
|
|
'labels' => $labels,
|
|
'public' => false,
|
|
'menu_position' => 4,
|
|
'menu_icon' => SHEET_CHECKER_PRO_URL .'assets/icons8-validation-menu-icon.png',
|
|
'supports' => array( 'title' ),
|
|
'hierarchical' => true,
|
|
'taxonomies' => array( 'category' ),
|
|
'has_archive' => false,
|
|
'rewrite' => array( 'slug' => 'checkers' ),
|
|
'show_ui' => true,
|
|
'show_in_menu' => true,
|
|
'show_in_rest' => false,
|
|
'query_var' => true,
|
|
);
|
|
|
|
register_post_type( 'checker', $args );
|
|
|
|
}
|
|
|
|
public function enqueue_bootstrap_admin() {
|
|
$screen = get_current_screen();
|
|
|
|
// Check that we are on the 'Checker' post editor screen
|
|
if ( $screen && $screen->id === 'checker' ) {
|
|
// Enqueue Bootstrap CSS
|
|
wp_enqueue_style( 'bootstrap', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css' );
|
|
// wp_enqueue_style( 'bs-table', 'https://unpkg.com/bootstrap-table@1.22.1/dist/bootstrap-table.min.css' );
|
|
wp_enqueue_style( 'bs-icon', 'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css' );
|
|
wp_enqueue_style( 'checker-editor', SHEET_CHECKER_PRO_URL . 'assets/admin-editor.css' );
|
|
wp_enqueue_style( 'datatables', 'https://cdn.datatables.net/2.2.2/css/dataTables.dataTables.css' );
|
|
|
|
// Enqueue Bootstrap JS
|
|
wp_enqueue_script( 'bootstrap', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js', array( 'jquery' ), '4.5.2', true );
|
|
wp_enqueue_script( 'handlebarjs', 'https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.8/handlebars.min.js', ['jquery'], '4.7.8', true );
|
|
// wp_enqueue_script( 'bs-table', 'https://unpkg.com/bootstrap-table@1.22.1/dist/bootstrap-table.min.js', ['jquery'], '1.22.1', true );
|
|
wp_enqueue_script( 'checker-editor', SHEET_CHECKER_PRO_URL . 'assets/admin-editor-interactions.js', ['jquery', 'handlebarjs'], true );
|
|
wp_enqueue_script( 'datatables', 'https://cdn.datatables.net/2.2.2/js/dataTables.js', ['jquery'], true );
|
|
wp_enqueue_script( 'datatables', 'https://cdn.datatables.net/responsive/3.0.4/js/dataTables.responsive.js', ['jquery'], true );
|
|
wp_enqueue_script( 'datatables', 'https://cdn.datatables.net/responsive/3.0.4/js/responsive.dataTables.js', ['jquery'], true );
|
|
}
|
|
|
|
wp_enqueue_style( 'checker-editor', SHEET_CHECKER_PRO_URL . 'assets/admin.css' );
|
|
}
|
|
|
|
public function filter_cpt_columns( $columns ) {
|
|
// this will add the column to the end of the array
|
|
$columns['shortcode'] = 'Shortcode';
|
|
//add more columns as needed
|
|
|
|
// as with all filters, we need to return the passed content/variable
|
|
return $columns;
|
|
}
|
|
|
|
public function action_custom_columns_content ( $column_id, $post_id ) {
|
|
//run a switch statement for all of the custom columns created
|
|
switch( $column_id ) {
|
|
case 'shortcode':
|
|
echo '<input class="dw-checker-post-table-input" value=\'[checker id="'.$post_id.'"]\' />';
|
|
break;
|
|
|
|
//add more items here as needed, just make sure to use the column_id in the filter for each new item.
|
|
|
|
}
|
|
}
|
|
|
|
public function add_checker_metabox() {
|
|
|
|
add_meta_box(
|
|
'checker_preview',
|
|
'Preview',
|
|
[$this, 'preview_checker_metabox'],
|
|
'checker',
|
|
'normal',
|
|
'default'
|
|
);
|
|
|
|
add_meta_box(
|
|
'checker_options',
|
|
'Options',
|
|
[$this, 'render_checker_metabox'],
|
|
'checker',
|
|
'normal',
|
|
'default'
|
|
);
|
|
|
|
if(isset($_GET['post']) && isset($_GET['action'])){
|
|
add_meta_box(
|
|
'checker_shortcode',
|
|
'Shortcode',
|
|
[$this, 'shortcode_checker_metabox'],
|
|
'checker',
|
|
'side',
|
|
'high'
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
public function shortcode_checker_metabox() {
|
|
?>
|
|
<div class="mb-2">Use shortcode below:</div>
|
|
<input value='[checker id="<?=$_GET['post']?>"]' class="form-control border-dark" readonly>
|
|
<?php
|
|
}
|
|
|
|
public function preview_checker_metabox($post) {
|
|
$checker = get_post_meta( $post->ID, 'checker', true );
|
|
$checker = wp_parse_args( $checker, [
|
|
'link' => '',
|
|
'description' => '',
|
|
'card' => [
|
|
'width' => 500,
|
|
'background' => '#cccccc',
|
|
'border_radius' => 1,
|
|
'box_shadow' => '10px 5px 15px -5px',
|
|
'box_shadow_color' => '#333333',
|
|
'title' => '#333333',
|
|
'title_align' => 'left',
|
|
'description' => '#333333',
|
|
'description_align' => 'left',
|
|
'divider' => '#333333',
|
|
'divider_width' => 1
|
|
],
|
|
'field' => [
|
|
'label' => 'block',
|
|
'label-color' => '#333333'
|
|
],
|
|
'fields' => [],
|
|
'search_button' => [
|
|
'text' => 'Search',
|
|
'bg_color' => '#cccccc',
|
|
'text_color' => '#333333',
|
|
'position' => 'flex-end'
|
|
],
|
|
'back_button' => [
|
|
'text' => 'Back',
|
|
'bg_color' => '#cccccc',
|
|
'text_color' => '#333333',
|
|
'position' => 'flex-start'
|
|
],
|
|
'result' => [
|
|
'display' => 'tabel',
|
|
'header' => '#333333',
|
|
'value' => '#333333',
|
|
'columns' => [],
|
|
'border_width' => 1
|
|
]
|
|
] );
|
|
|
|
require_once SHEET_CHECKER_PRO_PATH . 'templates/editor/preview.php';
|
|
}
|
|
|
|
public function render_checker_metabox( $post ) {
|
|
// Retrieve existing values from the database
|
|
$checker = get_post_meta( $post->ID, 'checker', true );
|
|
$checker = wp_parse_args( $checker, [
|
|
'link' => '',
|
|
'description' => '',
|
|
'card' => [
|
|
'width' => 500,
|
|
'background' => '#cccccc',
|
|
'bg_opacity' => 100,
|
|
'border_radius' => 1,
|
|
'box_shadow' => '10px 5px 15px -5px',
|
|
'box_shadow_color' => '#333333',
|
|
'title' => '#333333',
|
|
'title_align' => 'left',
|
|
'description' => '#333333',
|
|
'description_align' => 'left',
|
|
'divider' => '#333333',
|
|
'divider_width' => 1
|
|
],
|
|
'field' => [
|
|
'label' => 'block',
|
|
'label-color' => '#333333'
|
|
],
|
|
'fields' => [],
|
|
'search_button' => [
|
|
'text' => 'Search',
|
|
'bg_color' => '#cccccc',
|
|
'text_color' => '#333333',
|
|
'position' => 'flex-end'
|
|
],
|
|
'back_button' => [
|
|
'text' => 'Back',
|
|
'bg_color' => '#cccccc',
|
|
'text_color' => '#333333',
|
|
'position' => 'flex-start'
|
|
],
|
|
'result' => [
|
|
'display' => 'table',
|
|
'header' => '#333333',
|
|
'value' => '#333333',
|
|
'divider' => '#333333',
|
|
'divider_width' => 1
|
|
]
|
|
] );
|
|
|
|
require_once SHEET_CHECKER_PRO_PATH . 'templates/editor/settings.php';
|
|
}
|
|
|
|
public function save_checker_metabox( $post_id ) {
|
|
// Save metabox data
|
|
if ( isset( $_POST['checker'] ) ) {
|
|
error_log(print_r($_POST['checker'], true));
|
|
update_post_meta( $post_id, 'checker', $_POST['checker'] );
|
|
}
|
|
}
|
|
|
|
public function load_repeater_field_card_depracated() {
|
|
|
|
$post_id = $_REQUEST['pid'];
|
|
$checker = get_post_meta( $post_id, 'checker', true );
|
|
$json = json_decode(stripslashes($_REQUEST['json']), true);
|
|
|
|
if(isset($checker['fields']) && count($checker['fields']) > 0){
|
|
foreach($checker['fields'] as $key => $field){
|
|
?>
|
|
<div class="card shadow repeater-card gap-2 position-relative">
|
|
<div class="card-body">
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Field ID</label></div>
|
|
<div class="col-9">
|
|
<input class="form-control field-id" value="<?= $key ?>" />
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Column</label></div>
|
|
<div class="col-9">
|
|
<select name="checker[fields][<?= $key ?>][kolom]" class="form-select border select-kolom">
|
|
<?php
|
|
if($json){
|
|
$header = $this->parse_header_kolom($json);
|
|
if(!empty($header)){
|
|
foreach($header as $name){
|
|
if( $field['kolom'] == $name ){
|
|
echo '<option value="'.$name.'" selected>'.$name.'</option>';
|
|
}else{
|
|
echo '<option value="'.$name.'">'.$name.'</option>';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Type</label></div>
|
|
<div class="col-9">
|
|
<select name="checker[fields][<?= $key ?>][type]" class="form-select border select-field-type">
|
|
<option value="text" <?= ($field['type'] == 'text') ? 'selected' : '' ?>>Text</option>
|
|
<option value="select" <?= ($field['type'] == 'select') ? 'selected' : '' ?>>Select</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Label</label></div>
|
|
<div class="col-9">
|
|
<input name="checker[fields][<?= $key ?>][label]" class="form-control field-label" value="<?= $field['label'] ?? '' ?>" />
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Placeholder</label></div>
|
|
<div class="col-9">
|
|
<input name="checker[fields][<?= $key ?>][placeholder]" class="form-control field-placeholder" value="<?= $field['placeholder'] ?? '' ?>" />
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Value Matcher</label></div>
|
|
<div class="col-9">
|
|
<select name="checker[fields][<?= $key ?>][match]" class="form-select border select-match-type">
|
|
<option value="match" <?= ($field['match'] == 'match') ? 'selected' : '' ?>>Match</option>
|
|
<option value="contain" <?= ($field['match'] == 'contain') ? 'selected' : '' ?>>Contain</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="card-buttons d-flex gap-2 flex-column position-absolute">
|
|
<button type="button" class="btn btn-primary py-1 px-2 add-form-card"><i class="bi bi-plus"></i></button>
|
|
<button type="button" class="btn btn-danger py-1 px-2 delete-form-card"><i class="bi bi-dash"></i></button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
}else{
|
|
?>
|
|
<div class="card shadow repeater-card gap-2 position-relative">
|
|
<div class="card-body">
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Field ID</label></div>
|
|
<div class="col-9">
|
|
<input class="form-control field-id" value="" />
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Column</label></div>
|
|
<div class="col-9">
|
|
<select name="" class="form-select border select-kolom">
|
|
<?php
|
|
if($json){
|
|
$header = $this->parse_header_kolom($json);
|
|
if(!empty($header)){
|
|
foreach($header as $key => $name){
|
|
if( $key == 0 ){
|
|
echo '<option value="'.$name.'" selected>'.$name.'</option>';
|
|
}else{
|
|
echo '<option value="'.$name.'">'.$name.'</option>';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Type</label></div>
|
|
<div class="col-9">
|
|
<select name="" class="form-select border select-field-type">
|
|
<option value="text" selected>Text</option>
|
|
<option value="select">Select</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Label</label></div>
|
|
<div class="col-9">
|
|
<input name="" class="form-control field-label" value="" />
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Placeholder</label></div>
|
|
<div class="col-9">
|
|
<input name="" class="form-control field-placeholder" value="" />
|
|
</div>
|
|
</div>
|
|
<div class="row mb-2">
|
|
<div class="col-3"><label class="form-label fw-bold mb-0">Value Matcher</label></div>
|
|
<div class="col-9">
|
|
<select name="" class="form-select border select-match-type">
|
|
<option value="match" selected>Match</option>
|
|
<option value="contain">Contain</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="card-buttons d-flex gap-2 flex-column position-absolute">
|
|
<button type="button" class="btn btn-primary py-1 px-2 add-form-card"><i class="bi bi-plus"></i></button>
|
|
<button type="button" class="btn btn-danger py-1 px-2 delete-form-card"><i class="bi bi-dash"></i></button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
exit();
|
|
}
|
|
|
|
public function load_repeater_field_card() {
|
|
$post_id = $_REQUEST['pid'];
|
|
$checker = get_post_meta($post_id, 'checker', true);
|
|
$headers = $_REQUEST['headers'];
|
|
|
|
$response = [];
|
|
|
|
if (isset($checker['fields']) && count($checker['fields']) > 0) {
|
|
foreach ($checker['fields'] as $key => $field) {
|
|
$response[$key] = $field;
|
|
|
|
$rowHeader = [];
|
|
foreach($headers as $index => $header){
|
|
$id = '_'.strtolower($header);
|
|
$rowHeader[$index] = $id;
|
|
}
|
|
$response[$key]['selected_kolom'] = $response[$key]['kolom'];
|
|
$response[$key]['kolom'] = $headers;
|
|
}
|
|
}
|
|
|
|
wp_send_json($response);
|
|
exit();
|
|
}
|
|
|
|
public function load_column_checkbox() {
|
|
|
|
$post_id = $_REQUEST['pid'];
|
|
$checker = get_post_meta( $post_id, 'checker', true );
|
|
$json = json_decode(stripslashes($_REQUEST['json']), true);
|
|
|
|
$header = $this->parse_header_kolom($json);
|
|
|
|
if(count($header) > 0){
|
|
foreach($header as $key){
|
|
$checked = '';
|
|
if(isset($checker['result']['columns']) && in_array($key, $checker['result']['columns'])){
|
|
$checked = ' checked';
|
|
}
|
|
?>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" value="<?= $key ?>" id="checker-item-<?= strtolower(str_replace(' ', '_', $key)) ?>" name="checker[result][columns][]"<?=$checked?>>
|
|
<label class="form-check-label" for="checker-item-<?= strtolower(str_replace(' ', '_', $key)) ?>">
|
|
<?= $key ?>
|
|
</label>
|
|
</div>
|
|
<?php
|
|
}
|
|
}
|
|
exit();
|
|
|
|
}
|
|
|
|
public function load_output_setting() {
|
|
$post_id = $_REQUEST['pid'];
|
|
$checker = get_post_meta($post_id, 'checker', true);
|
|
$headers = $_REQUEST['headers'];
|
|
|
|
// $header = $this->parse_header_kolom($json);
|
|
|
|
if (!empty($headers)) {
|
|
$output_data = [];
|
|
|
|
foreach ($headers as $key) {
|
|
$id = strtolower(str_replace([' ', '.'], '_', $key));
|
|
|
|
$output_data[] = [
|
|
'key' => $key,
|
|
'id' => $id,
|
|
'hide' => isset($checker['output'][$id]['hide']) ? $checker['output'][$id]['hide'] : 'no',
|
|
'type' => isset($checker['output'][$id]['type']) ? $checker['output'][$id]['type'] : 'text',
|
|
'button_text' => isset($checker['output'][$id]['button_text']) ? $checker['output'][$id]['button_text'] : '',
|
|
'prefix' => isset($checker['output'][$id]['prefix']) ? $checker['output'][$id]['prefix'] : '',
|
|
'bg_color' => isset($checker['output'][$id]['bg_color']) ? $checker['output'][$id]['bg_color'] : '#cccccc',
|
|
'text_color' => isset($checker['output'][$id]['text_color']) ? $checker['output'][$id]['text_color'] : '#000000',
|
|
'display' => isset($checker['result']['display']) && $checker['result']['display'] == 'card'
|
|
];
|
|
}
|
|
|
|
wp_send_json_success(['data' => $output_data]);
|
|
} else {
|
|
wp_send_json_error('No headers found');
|
|
}
|
|
exit();
|
|
}
|
|
|
|
|
|
public function parse_options($json, $kolom) {
|
|
|
|
$json = json_decode($json, true);
|
|
$options = [];
|
|
if($json){
|
|
foreach($json as $key => $value){
|
|
foreach($value as $name => $val){
|
|
if($name == $kolom){
|
|
if(!in_array($val, $options)){
|
|
$options[] = $val;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
public function parse_header_kolom($json) {
|
|
|
|
$header = [];
|
|
if(!is_array($json)){
|
|
$json = json_decode($json, true);
|
|
}
|
|
$header = array_keys($json[0]);
|
|
return $header;
|
|
|
|
}
|
|
|
|
}
|