cookie-law-info
/
legacy
/
admin
/
modules
/
cli-policy-generator
/
classes
/
class-policy-generator-ajax.php
class-policy-generator-ajax.php
139 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | class Cookie_Law_Info_Policy_Generator_Ajax extends Cookie_Law_Info_Cli_Policy_Generator { |
| 6 | |
| 7 | |
| 8 | public function __construct() { |
| 9 | add_action( 'wp_ajax_cli_policy_generator', array( $this, 'ajax_policy_generator' ) ); |
| 10 | } |
| 11 | |
| 12 | /* |
| 13 | * |
| 14 | * Main Ajax hook for processing requests |
| 15 | */ |
| 16 | public function ajax_policy_generator() { |
| 17 | check_ajax_referer( 'cli_policy_generator', 'security' ); |
| 18 | if ( ! current_user_can( 'manage_options' ) ) { |
| 19 | wp_die( esc_html__( 'You do not have sufficient permission to perform this operation', 'cookie-law-info' ) ); |
| 20 | } |
| 21 | $out = array( |
| 22 | 'response' => false, |
| 23 | 'message' => __( 'Unable to handle your request.', 'cookie-law-info' ), |
| 24 | ); |
| 25 | $non_json_response = array(); |
| 26 | if ( isset( $_POST['cli_policy_generator_action'] ) ) { |
| 27 | $allowed_actions = array( 'autosave_content_data', 'save_contentdata', 'get_policy_pageid' ); |
| 28 | $action = isset( $_POST['cli_policy_generator_action'] ) ? sanitize_text_field( wp_unslash( $_POST['cli_policy_generator_action'] ) ) : ''; |
| 29 | $cli_policy_generator_action = in_array( $action, $allowed_actions ) ? $action : ''; |
| 30 | if ( in_array( $cli_policy_generator_action, $allowed_actions ) && method_exists( $this, $cli_policy_generator_action ) ) { |
| 31 | $out = $this->{$cli_policy_generator_action}(); |
| 32 | } |
| 33 | } |
| 34 | if ( in_array( $cli_policy_generator_action, $non_json_response ) ) { |
| 35 | echo esc_html( is_array( $out ) ? $out['message'] : $out ); |
| 36 | } else { |
| 37 | echo wp_json_encode( $out ); |
| 38 | } |
| 39 | exit(); |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * @since 1.7.4 |
| 44 | * Get current policy page ID (Ajax-main) |
| 45 | * This is used to update the hidden field for policy page id. (In some case user press back button) |
| 46 | */ |
| 47 | public function get_policy_pageid() { |
| 48 | $page_id = Cookie_Law_Info_Cli_Policy_Generator::get_cookie_policy_pageid(); |
| 49 | $policy_page_status = get_post_status( $page_id ); |
| 50 | if ( $policy_page_status && $policy_page_status != 'trash' ) { |
| 51 | |
| 52 | } else { |
| 53 | $page_id = 0; |
| 54 | } |
| 55 | return array( |
| 56 | 'response' => true, |
| 57 | 'page_id' => $page_id, |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * @since 1.7.4 |
| 63 | * Save current data (Ajax-main) |
| 64 | */ |
| 65 | public function save_contentdata() { |
| 66 | check_ajax_referer( 'cli_policy_generator', 'security' ); |
| 67 | $out = array( |
| 68 | 'response' => true, |
| 69 | 'er' => '', |
| 70 | ); |
| 71 | $content_data = array(); |
| 72 | $count = isset( $_POST['content_data'] ) ? count( $_POST['content_data'] ) : 0; |
| 73 | for ( $i = 0; $i < $count; $i++ ) { |
| 74 | $data = array( |
| 75 | 'ord' => isset( $_POST['content_data'][ $i ]['ord'] ) ? absint( $_POST['content_data'][ $i ]['ord'] ) : 0, |
| 76 | 'hd' => isset( $_POST['content_data'][ $i ]['hd'] ) ? sanitize_text_field( wp_unslash( $_POST['content_data'][ $i ]['hd'] ) ) : '', |
| 77 | 'content' => isset( $_POST['content_data'][ $i ]['content'] ) ? wp_kses_post( wp_unslash( $_POST['content_data'][ $i ]['content'] ) ) : '', |
| 78 | ); |
| 79 | $content_data[] = $data; |
| 80 | } |
| 81 | $page_id = isset( $_POST['page_id'] ) ? absint( $_POST['page_id'] ) : 0; |
| 82 | $enable_webtofee_powered_by = isset( $_POST['enable_webtofee_powered_by'] ) ? absint( $_POST['enable_webtofee_powered_by'] ) : 0; |
| 83 | $id = wp_insert_post( |
| 84 | array( |
| 85 | 'ID' => $page_id, // if ID is zero it will create new page otherwise update |
| 86 | 'post_title' => 'Cookie Policy', |
| 87 | 'post_type' => 'page', |
| 88 | 'post_content' => Cookie_Law_Info_Cli_Policy_Generator::generate_page_content( $enable_webtofee_powered_by, $content_data, 0 ), |
| 89 | 'post_status' => 'draft', // default is draft |
| 90 | ) |
| 91 | ); |
| 92 | if ( is_wp_error( $id ) ) { |
| 93 | $out = array( |
| 94 | 'response' => false, |
| 95 | 'er' => __( 'Error', 'cookie-law-info' ), |
| 96 | ); |
| 97 | } else { |
| 98 | Cookie_Law_Info_Cli_Policy_Generator::set_cookie_policy_pageid( $id ); |
| 99 | $out['url'] = get_edit_post_link( $id ); |
| 100 | } |
| 101 | return $out; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * @since 1.7.4 |
| 106 | * Autosave Current content to session (Ajax-main) |
| 107 | */ |
| 108 | public function autosave_content_data() { |
| 109 | check_ajax_referer( 'cli_policy_generator', 'security' ); |
| 110 | $out = array( |
| 111 | 'response' => true, |
| 112 | 'er' => '', |
| 113 | ); |
| 114 | $content_data = array(); |
| 115 | $count = isset( $_POST['content_data'] ) ? count( $_POST['content_data'] ) : 0; |
| 116 | for ( $i = 0; $i < $count; $i++ ) { |
| 117 | $data = array( |
| 118 | 'ord' => isset( $_POST['content_data'][ $i ]['ord'] ) ? absint( $_POST['content_data'][ $i ]['ord'] ) : 0, |
| 119 | 'hd' => isset( $_POST['content_data'][ $i ]['hd'] ) ? sanitize_text_field( wp_unslash( $_POST['content_data'][ $i ]['hd'] ) ) : '', |
| 120 | 'content' => isset( $_POST['content_data'][ $i ]['content'] ) ? wp_kses_post( wp_unslash( $_POST['content_data'][ $i ]['content'] ) ) : '', |
| 121 | ); |
| 122 | $content_data[] = $data; |
| 123 | } |
| 124 | $page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0; |
| 125 | $enable_webtofee_powered_by = isset( $_POST['enable_webtofee_powered_by'] ) ? intval( $_POST['enable_webtofee_powered_by'] ) : 0; |
| 126 | if ( is_array( $content_data ) ) { |
| 127 | $content_html = Cookie_Law_Info_Cli_Policy_Generator::generate_page_content( $enable_webtofee_powered_by, $content_data ); |
| 128 | update_option( 'cli_pg_content_data', $content_html ); |
| 129 | } else { |
| 130 | $out = array( |
| 131 | 'response' => false, |
| 132 | 'er' => __( 'Error', 'cookie-law-info' ), |
| 133 | ); |
| 134 | } |
| 135 | return $out; |
| 136 | } |
| 137 | } |
| 138 | new Cookie_Law_Info_Policy_Generator_Ajax(); |
| 139 |