| 1 |
<?php |
| 2 |
|
| 3 |
if( ! class_exists( 'Chart_Builder_Settings_DB_Actions' ) ){ |
| 4 |
ob_start(); |
| 5 |
|
| 6 |
/** |
| 7 |
* Class Chart_Builder_Settings_DB_Actions |
| 8 |
* Class contains functions to interact with settings database |
| 9 |
* |
| 10 |
* Main functionality belong to inserting, updating and deleting |
| 11 |
* |
| 12 |
* Hooks used in the class |
| 13 |
* @hooks @filters ays_cb_settings_page_integrations_saves |
| 14 |
* ays_chart_item_save_settings |
| 15 |
* |
| 16 |
* Database tables without prefixes |
| 17 |
* @tables settings |
| 18 |
* |
| 19 |
* @param $plugin_name |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
* @package Chart_Builder |
| 23 |
* @subpackage Chart_Builder/includes |
| 24 |
* @author Chart Builder Team <info@ays-pro.com> |
| 25 |
*/ |
| 26 |
class Chart_Builder_Settings_DB_Actions { |
| 27 |
|
| 28 |
/** |
| 29 |
* The ID of this plugin. |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
* @access private |
| 33 |
* @var string $plugin_name The ID of this plugin. |
| 34 |
*/ |
| 35 |
private $plugin_name; |
| 36 |
|
| 37 |
/** |
| 38 |
* The name of table in the database. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
* @access private |
| 42 |
* @var string $db_table The name of database table. |
| 43 |
*/ |
| 44 |
private $db_table; |
| 45 |
|
| 46 |
/** |
| 47 |
* The constructor of the class |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* @access public |
| 51 |
* |
| 52 |
* @param $plugin_name |
| 53 |
*/ |
| 54 |
public function __construct( $plugin_name ) { |
| 55 |
|
| 56 |
global $wpdb; |
| 57 |
|
| 58 |
/** |
| 59 |
* Assigning $plugin_name to the @plugin_name property |
| 60 |
*/ |
| 61 |
$this->plugin_name = $plugin_name; |
| 62 |
|
| 63 |
/** |
| 64 |
* Assigning database @charts table full name to the @db_table property |
| 65 |
*/ |
| 66 |
$this->db_table = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . "settings"; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get instance of this class |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* @access public |
| 75 |
* |
| 76 |
* @param $plugin_name |
| 77 |
* |
| 78 |
* @return Chart_Builder_DB_Actions |
| 79 |
*/ |
| 80 |
public static function get_instance( $plugin_name ){ |
| 81 |
return new self( $plugin_name ); |
| 82 |
} |
| 83 |
|
| 84 |
public function store_data(){ |
| 85 |
|
| 86 |
if( isset( $_REQUEST["settings_action"] ) && wp_verify_nonce( sanitize_text_field(wp_unslash($_REQUEST["settings_action"])), 'settings_action' ) ){ |
| 87 |
$success = 0; |
| 88 |
$name_prefix = 'ays_'; |
| 89 |
|
| 90 |
$user_roles = (isset($_REQUEST['ays_user_roles']) && !empty($_REQUEST['ays_user_roles'])) ? array_map( 'sanitize_text_field', wp_unslash($_REQUEST['ays_user_roles']) ) : array('administrator'); |
| 91 |
|
| 92 |
// User roles to change plugin |
| 93 |
$user_roles_to_change_plugin = (isset($_REQUEST[$name_prefix . 'user_roles_to_change_plugin']) && !empty( $_REQUEST[$name_prefix . 'user_roles_to_change_plugin'] ) ) ? array_map( 'sanitize_text_field',wp_unslash( $_REQUEST[$name_prefix . 'user_roles_to_change_plugin'] )) : array('administrator'); |
| 94 |
|
| 95 |
// // Do not store IP addresses |
| 96 |
// $disable_user_ip = (isset($_REQUEST[$name_prefix . 'chart_disable_user_ip']) && $_REQUEST[$name_prefix . 'chart_disable_user_ip'] == 'on') ? stripslashes( sanitize_text_field( $_REQUEST[$name_prefix . 'chart_disable_user_ip'] ) ) : ''; |
| 97 |
|
| 98 |
$chart_title_length = (isset($_REQUEST[$name_prefix . 'chart_title_length']) && $_REQUEST[$name_prefix . 'chart_title_length'] != '') ? absint( sanitize_text_field( wp_unslash($_REQUEST[$name_prefix . 'chart_title_length'] )) ) : 5; |
| 99 |
|
| 100 |
// // Textarea height (public) |
| 101 |
// $textarea_height = (isset($_REQUEST[$name_prefix . 'chart_textarea_height']) && $_REQUEST[$name_prefix . 'chart_textarea_height'] != '' && $_REQUEST[$name_prefix . 'chart_textarea_height'] != 0 ) ? absint( sanitize_text_field($_REQUEST[$name_prefix . 'chart_textarea_height']) ) : 100; |
| 102 |
|
| 103 |
// // WP Editor height |
| 104 |
// $wp_editor_height = (isset($_REQUEST[$name_prefix . 'chart_wp_editor_height']) && $_REQUEST[$name_prefix . 'chart_wp_editor_height'] != '' && $_REQUEST[$name_prefix . 'chart_wp_editor_height'] != 0) ? absint( sanitize_text_field($_REQUEST[$name_prefix . 'chart_wp_editor_height']) ) : 100 ; |
| 105 |
|
| 106 |
$options = array( |
| 107 |
// "disable_user_ip" => $disable_user_ip, |
| 108 |
"title_length" => $chart_title_length, |
| 109 |
// "textarea_height" => $textarea_height, |
| 110 |
// "wp_editor_height" => $wp_editor_height, |
| 111 |
|
| 112 |
// User roles options |
| 113 |
"user_roles_to_access" => $user_roles, |
| 114 |
"user_roles_to_change" => $user_roles_to_change_plugin, |
| 115 |
); |
| 116 |
|
| 117 |
$fields = array(); |
| 118 |
|
| 119 |
$fields['user_roles'] = $user_roles; |
| 120 |
$fields['options'] = $options; |
| 121 |
|
| 122 |
$fields = apply_filters( 'ays_cb_settings_page_integrations_saves', $fields ); |
| 123 |
|
| 124 |
foreach ($fields as $key => $value) { |
| 125 |
$result = $this->update_setting( $key, json_encode( $value ) ); |
| 126 |
if($result){ |
| 127 |
$success++; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
$message = "saved"; |
| 132 |
if($success > 0){ |
| 133 |
$tab = ""; |
| 134 |
if( isset( $_REQUEST['ays_tab'] ) ){ |
| 135 |
$tab = "&ays_tab=". sanitize_text_field( wp_unslash($_REQUEST['ays_tab'] )); |
| 136 |
} |
| 137 |
|
| 138 |
$url = admin_url('admin.php') . "?page=". $this->plugin_name ."-settings" . $tab . '&status=' . $message; |
| 139 |
wp_redirect( $url ); |
| 140 |
exit(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @return array |
| 148 |
*/ |
| 149 |
public function get_all_data(){ |
| 150 |
global $wpdb; |
| 151 |
|
| 152 |
$sql = "SELECT * FROM " . $this->db_table; |
| 153 |
|
| 154 |
$results = $wpdb->get_results($sql, ARRAY_A); |
| 155 |
|
| 156 |
if( count( $results ) > 0 ){ |
| 157 |
return $results; |
| 158 |
}else{ |
| 159 |
return array(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get record meta by record id and meta key |
| 165 |
* |
| 166 |
* @since 1.0.0 |
| 167 |
* @access public |
| 168 |
* |
| 169 |
* @param $id |
| 170 |
* @param $meta_key |
| 171 |
* |
| 172 |
* @return false|array |
| 173 |
*/ |
| 174 |
public function get_setting( $meta_key ){ |
| 175 |
global $wpdb; |
| 176 |
|
| 177 |
if( is_null( $meta_key ) || trim( $meta_key ) === '' ){ |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
$sql = "SELECT meta_value FROM {$this->db_table} WHERE meta_key = %s"; |
| 182 |
// phpcs:ignore |
| 183 |
$result = $wpdb->get_var($wpdb->prepare($sql, $meta_key)); |
| 184 |
|
| 185 |
if($result != ""){ |
| 186 |
return $result; |
| 187 |
} |
| 188 |
|
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
public function update_setting( $meta_key, $meta_value, $note = "", $options = "" ){ |
| 193 |
global $wpdb; |
| 194 |
|
| 195 |
if( is_null( $meta_key ) || trim( $meta_key ) === '' ){ |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
$value = array( |
| 200 |
'meta_value' => $meta_value,// phpcs:ignore |
| 201 |
); |
| 202 |
|
| 203 |
$value_s = array( '%s' ); |
| 204 |
if($note != null){ |
| 205 |
$value['note'] = $note; |
| 206 |
$value_s[] = '%s'; |
| 207 |
} |
| 208 |
|
| 209 |
if($options != null){ |
| 210 |
$value['options'] = $options; |
| 211 |
$value_s[] = '%s'; |
| 212 |
} |
| 213 |
|
| 214 |
$result = $wpdb->update(// phpcs:ignore |
| 215 |
$this->db_table, |
| 216 |
$value, |
| 217 |
array( |
| 218 |
'meta_key' => $meta_key,// phpcs:ignore |
| 219 |
), |
| 220 |
$value_s, |
| 221 |
array( '%s' ) |
| 222 |
); |
| 223 |
|
| 224 |
if($result >= 0){ |
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
public function get_listtables_title_length() { |
| 232 |
global $wpdb; |
| 233 |
|
| 234 |
$sql = "SELECT meta_value FROM {$this->db_table} WHERE meta_key = %s"; |
| 235 |
// phpcs:ignore |
| 236 |
$result = $wpdb->get_var($wpdb->prepare($sql, 'options')); |
| 237 |
|
| 238 |
$options = ($result == "") ? array() : json_decode(stripcslashes($result), true); |
| 239 |
|
| 240 |
$listtable_title_length = 5; |
| 241 |
if( !empty($options) ){ |
| 242 |
$listtable_title_length = (isset($options['title_length']) && intval($options['title_length']) != 0) ? absint(intval($options['title_length'])) : 5; |
| 243 |
} |
| 244 |
return $listtable_title_length; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|