/wp-content/plugins/admin-block-country/images/facebook.jpg" style="width: 30px;" /> /wp-content/plugins/admin-block-country/images/twitter.jpg" style="width: 30px;" /> /wp-content/plugins/admin-block-country/images/rate-me.png" /> \norder allow,deny\ndeny from all\n\nphp_flag log_errors on\nphp_value error_log error_log"); function write_to_htaccess_file($rule_name, $content) { $htaccess_content = file_get_contents(ABSPATH.".htaccess"); $htaccess_content = preg_replace("/\n#BEGIN ".$rule_name."(.+)#END ".$rule_name."/s", "", $htaccess_content); file_put_contents(ABSPATH.".htaccess", $htaccess_content); $new_content = "\n#BEGIN ".$rule_name. "\n".$content."\n". "#END ".$rule_name; file_put_contents(ABSPATH.".htaccess", $new_content, FILE_APPEND | LOCK_EX); } // Returns true if the file is writable, false if it isn't. function is_file_writable($file) { if ( $f = @fopen( $file, 'a' ) ) { @fclose( $f ); return true; } else { return false; } } // Returns true if the file is readable, false if it isn't. function is_file_readable($file) { if ( $f = @fopen( $file, 'r' ) ) { @fclose( $f ); return true; } else { return false; } } // Javascript redirect to url code. function javascript_redirect_to($url, $non_javscript_content = "") { echo(""); if ($non_javscript_content != "") { echo $non_javscript_content; } } // Titlizes a string. For example: status_level would become Status Level. function titlize_str($str) { return ucwords((str_replace("_", " ", $str))); } // Return current url. function get_current_url() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } // Returns true if parameter is a datetime variable. function is_valid_datetime($datetime) { return (preg_match("/^([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})( ([0-9| |:])*)*$/", $datetime)); } // Returns true if parameter is an email address. You can only pass one email address. function is_valid_email($email) { $email = strtolower($email); return (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email)); } // Returns true if parameter is an email address. You can pass more then one email address, by separating them with a comma. function is_valid_emails($emails) { $emails_valid = true; $email_addresses = explode(",", preg_replace("/,( )*/", ",",$emails)); foreach ($email_addresses as $email_address) { $email_address = strtolower($email_address); if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email_address)) { $emails_valid = false; } } return $emails_valid; } // Fixes up http post/get variables so that they present quotes correctly rather then like (\'). function fix_http_quotes($http_data) { $http_data = str_replace('\"', "\"", $http_data); $http_data = str_replace("\'", '\'', $http_data); return $http_data; } // Basically gets the value from query string without having to use $_POST or $_GET variables. $_POST takes precidence over $_GET. function get_query_string_value($name, $index = -1) { if ($index == -1) { if (isset($_POST[$name])) { return ABCTomM8::fix_http_quotes($_POST[$name]); } else if (isset($_GET[$name])) { return ABCTomM8::fix_http_quotes($_GET[$name]); } else if (isset($_POST[$name."_0"])) { $i = 0; $data = ""; do { $data .= $_POST[$name."_".$i]; if ($data != "") { $data .= " "; } $i++; } while (isset($_POST[$name."_".$i])); $_POST[$name] = ABCTomM8::fix_http_quotes($data); return ABCTomM8::fix_http_quotes($data); } else { return ""; } } else { $name = str_replace("[]", "", $name); if (isset($_POST[$name][$index])) { return ABCTomM8::fix_http_quotes($_POST[$name][$index]); } else if (isset($_GET[$name][$index])) { return ABCTomM8::fix_http_quotes($_GET[$name][$index]); } else if (isset($_POST[$name."_0"][$index])) { $i = 0; $data = ""; do { $data .= $_POST[$name."_".$i][$index]; if ($data != "") { $data .= " "; } $i++; } while (isset($_POST[$name."_".$i][$index])); $_POST[$name][$index] = ABCTomM8::fix_http_quotes($data); return ABCTomM8::fix_http_quotes($data); } else { return ""; } } } // Upload a file. function upload_file($field_name) { $uploadfiles = $_FILES[$field_name]; if (is_array($uploadfiles)) { foreach ($uploadfiles['name'] as $key => $value) { // look only for uploded files if ($uploadfiles['error'][$key] == 0) { $filetmp = $uploadfiles['tmp_name'][$key]; //clean filename and extract extension $filename = $uploadfiles['name'][$key]; // get file info // @fixme: wp checks the file extension.... $filetype = wp_check_filetype( basename( $filename ), null ); $filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) ); $filename = $filetitle . '.' . $filetype['ext']; $upload_dir = wp_upload_dir(); /** * Check if the filename already exist in the directory and rename the * file if necessary */ $i = 0; while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) { $filename = $filetitle . '_' . $i . '.' . $filetype['ext']; $i++; } $filedest = $upload_dir['path'] . '/' . $filename; /** * Check write permissions */ if ( !is_writeable( $upload_dir['path'] ) ) { $this->msg_e('Unable to write to directory %s. Is this directory writable by the server?'); return; } /** * Save temporary file to uploads dir */ if ( !@move_uploaded_file($filetmp, $filedest) ){ $this->msg_e("Error, the file $filetmp could not moved to : $filedest "); continue; } $attachment = array( 'post_mime_type' => $filetype['type'], 'post_title' => $filetitle, 'post_content' => '', 'post_status' => 'inherit', ); $attach_id = wp_insert_attachment( $attachment, $filedest ); $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest ); wp_update_attachment_metadata( $attach_id, $attach_data ); preg_match("/\/wp-content(.+)$/", $filedest, $matches, PREG_OFFSET_CAPTURE); ABCTomM8::update_record_by_id("posts", array("guid" => get_option("siteurl").$matches[0][0]), "ID", $attach_id); // echo $filedest; } } } } // Allows you to send an email. function send_email($is_html, $to_emails, $to_cc_emails, $to_bcc_emails, $from_email, $from_name, $subject, $body, $alt_body = "", $attachments = array(), $smtp_auth = false, $smtp_mail_host = "", $smtp_mail_port = "", $smtp_mail_username = "", $smtp_mail_password = "", $secure_array = array()) { $mail = new PHPMailer(); // defaults to using php "mail()" $body = preg_replace("/[\"]/","",$body); if (!is_array($to_emails)) { $to_emails = explode(",", $to_emails); } foreach ($to_emails as $key => $value) { if (is_integer($key)) { $mail->AddAddress(str_replace(" ", "",$value), ""); } else { $mail->AddAddress(str_replace(" ", "",$key), $value); } } if (!is_array($to_cc_emails)) { $to_cc_emails = explode(",", $to_cc_emails); } foreach ($to_cc_emails as $key => $value) { if (is_integer($key)) { $mail->AddCC(str_replace(" ", "",$value), ""); } else { $mail->AddCC(str_replace(" ", "",$key), $value); } } if (!is_array($to_bcc_emails)) { $to_bcc_emails = explode(",", $to_bcc_emails); } foreach ($to_bcc_emails as $key => $value) { if (is_integer($key)) { $mail->AddBCC(str_replace(" ", "",$value), ""); } else { $mail->AddBCC(str_replace(" ", "",$key), $value); } } $mail->SetFrom($from_email, $from_name); $mail->Subject = $subject; if ($is_html) { $body = preg_replace("//", "", $body); } else { $body = esc_html($body); } $alt_body = esc_html($alt_body); $body = str_replace("'", "'", $body); $body = str_replace(""", '"', $body); $body = str_replace("&", '&', $body); $body = str_replace("&", '&', $body); $alt_body = str_replace("'", "'", $alt_body); $alt_body = str_replace(""", '"', $alt_body); $alt_body = str_replace("&", '&', $alt_body); $alt_body = str_replace("&", '&', $alt_body); if ($is_html) { $mail->MsgHTML($body); } else { $mail->Body = $body; } if ($alt_body != "") { $mail->AltBody = $alt_body; } foreach ($attachments as $attachment_url) { $mail->AddAttachment($attachment_url); // attachment } if ($smtp_auth) { $mail->IsSMTP(); // telling the class to use SMTP $mail->SMTPAuth = true; if ($smtp_mail_host != "") { $mail->Host = $smtp_mail_host; } if ($smtp_mail_port != "") { $mail->Port = $smtp_mail_port; } if ($smtp_mail_username != "") { $mail->Username = $smtp_mail_username; } if ($smtp_mail_password != "") { $mail->Password = $smtp_mail_password; } foreach ($secure_array as $secure) { if ($secure == "tls") { $mail->SMTPSecure = 'tls'; } else if ($secure == "ssl") { $mail->SMTPSecure = 'ssl'; } } } if(!$mail->Send()) { return "
Mailer Error: ".$mail->ErrorInfo."
"; } else { return "
Message sent!
"; } } // Generates a datatable with show, edit and delete links. function generate_datatable($table_name, $fields_array, $primary_key_name, $where_clause, $order_array = array(), $limit_clause = "", $page_name, $display_show = true, $display_edit = true, $display_delete = true, $sortable_columns = false, $paginate_table = false, $date_format = "Y-m-d", $filter_arrays = array()) { if (!is_array($fields_array)) { echo("Fields Array, can only accept an array of field names."); } else { // Get the page no. Mainly used during pagination. if (isset($_GET[$table_name."_page"])) { $page_no = $_GET[$table_name."_page"]; } // If sort columns enabled, find out what order the columns are suppose to be in. if ($sortable_columns && isset($_GET[$table_name."_order_by"]) && $_GET[$table_name."_order_by"] != "") { array_unshift($order_array, $_GET[$table_name."_order_by"]." ".$_GET[$table_name."_order_direction"]); } if (isset($_GET[$table_name."_order_direction"]) && $_GET[$table_name."_order_direction"] != "") { $order_direction = $_GET[$table_name."_order_direction"]; } // Work out which page no of results to show. Offset is the same as page no in MySQL. $offset_clause = ""; if ($limit_clause != "" && $paginate_table) { $offset = 0; $offset = $page_no * $limit_clause; $offset_clause = " OFFSET $offset"; } // If filter enabled, add extra filter conditions to existing datatable. $extra_where = array(); if (count($filter_arrays) > 0) { if (ABCTomM8::get_query_string_value($table_name."_filters") != "") { $filters = explode(",", ABCTomM8::get_query_string_value($table_name."_filters")); foreach ($filters as $filter) { if (ABCTomM8::get_query_string_value("filter_".$filter) != "") { if (!(isset($_POST["action"]) && $_POST["action"] == "Reset")) { array_push($extra_where, $filter." LIKE '%".ABCTomM8::get_query_string_value("filter_".$filter)."%'"); } } } if (count($extra_where) > 0) { if ($where_clause != "") { $where_clause = "(".$where_clause.") AND (".implode(" AND ", $extra_where).")"; } else { $where_clause = implode(" AND ", $extra_where); } } } if (isset($_POST["action"]) && $_POST["action"] == "Filter") { $page_no = 0; $offset_clause = " OFFSET 0"; } } $results = ABCTomM8::get_results($table_name, $fields_array, $where_clause, $order_array, $limit_clause.$offset_clause); $total_count = count(ABCTomM8::get_results($table_name, $fields_array, $where_clause)); echo("
"); $filters = array(); if (count($filter_arrays) > 0) { ?>

Filter

$value) { if (ABCTomM8::get_query_string_value("filter_".$key) != "") { if (isset($_POST["action"]) && $_POST["action"] == "Reset") { $_POST["filter_".$key] = ""; } $params_filter .= "&filter_".$key."=".ABCTomM8::get_query_string_value("filter_".$key); } array_push($filters, $key); ABCTomM8::add_form_field(null, $value["type"], titlize_str($key), "filter_".$key, "filter_".$key, array(), "p", array(), $value["value_options"]); } } ?> " />

0) { if ($paginate_table) { ABCTomM8::generate_datatable_pagination($table_name, $total_count, $limit_clause, $page_no, $page_name, $order_direction, "top"); } ?>
$field_name)) { echo(date($date_format, strtotime($result->$field_name ))); } else { echo($result->$field_name); } ?> Show Edit Delete
0) { echo("

Sorry no records found, please try change your search preferences.

"); } else { echo("

Sorry no records found.

"); } } echo("
"); } } // This method is used by generate_datatable. Please don't use. function generate_datatable_pagination($table_name, $total_count, $limit_clause, $page_no, $page_name, $order_direction, $pagination_class) { if ($order_direction == "") { $order_direction = "ASC"; } $total_number_pages = intval($total_count / $limit_clause); $params_filter = ""; $filters = explode(",", ABCTomM8::get_query_string_value($table_name."_filters")); $params_filter .= "&".$table_name."_filters=".ABCTomM8::get_query_string_value($table_name."_filters"); foreach ($filters as $filter) { if (ABCTomM8::get_query_string_value("filter_".$filter) != "") { $params_filter .= "&filter_".$filter."=".ABCTomM8::get_query_string_value("filter_".$filter); } } ?> "); foreach($fields_array as $field) { echo("
".ucwords(esc_html(str_replace("_", " ", $field)))."
".esc_html($result->$field)."
"); } echo(""); } } // Returns compressed version of $content. function compress_content($content) { /* remove comments */ $content = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $content); /* remove tabs, spaces, newlines, etc. */ return str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), ' ', $content); } // Returns array of query string from a form. Works out the $_POST and $_GET array names from the database table column names. function get_form_query_strings($table_name, $exclude_fields = array(), $include_field_values = array()) { global $wpdb; $table_name_prefix = $wpdb->prefix . $table_name; $sql = "SHOW columns FROM ".$table_name_prefix; $results = $wpdb->get_results($sql); $return_array = array(); foreach ($results as $result) { if (!in_array($result->Field, $exclude_fields)) { $value = $_POST[$result->Field]; if (preg_match("/^decimal/i", $result->Type)) { $value = str_replace("$", "", $value); $value = str_replace(",", "", $value); } $return_array[$result->Field] = $value; } } return array_merge($return_array, $include_field_values); } // Returns true if value passes validation. Used by validate_form. // $validation = can either be required, integer, currency, date. // $value = is the value to test against. // $error_session_name = name of the session to store the error. function validate_value($validation, $value, $error_session_name) { $validate_form = true; if (preg_match("/required | required|^required$/i", $validation)) { if ($value == "") { $_SESSION[$error_session_name] .= " must have a value. "; $validate_form = false; } } if ($value != "") { if (preg_match("/integer | integer|^integer$/i", $validation)) { if (!is_numeric($value)) { if (!preg_match("/must be a number/", $_SESSION[$error_session_name])) { $_SESSION[$error_session_name] .= " must be a number. "; } $validate_form = false; } } if (preg_match("/currency | currency|^currency$/i", $validation)) { if (!preg_match("/^\\$?([0-9])+(,)?([0-9])*(,)?([0-9])*(\.)?([0-9]){1,2}?$/", $value)) { if (!preg_match("/must be a currency/", $_SESSION[$error_session_name])) { $_SESSION[$error_session_name] .= " must be a currency (e.g: $1,300,323.00). "; } $validate_form = false; } } if (preg_match("/date | date|^date$/i", $validation)) { if (!ABCTomM8::is_valid_datetime($value)) { if (!preg_match("/must be a date/", $_SESSION[$error_session_name])) { $_SESSION[$error_session_name] .= " must be a date. "; } $validate_form = false; } } if (preg_match("/email | email|^email$/i", $validation)) { if (!ABCTomM8::is_valid_email($value)) { if (!preg_match("/must be a valid email address/", $_SESSION[$error_session_name])) { $_SESSION[$error_session_name] .= " must be a valid email address. "; } $validate_form = false; } } if (preg_match("/multi-emails | multi-emails|^multi-emails$/i", $validation)) { if (!ABCTomM8::is_valid_emails($value)) { if (!preg_match("/must have valid email addressess, separated by commas/", $_SESSION[$error_session_name])) { $_SESSION[$error_session_name] .= " must have valid email addressess, separated by commas. "; } $validate_form = false; } } } return $validate_form; } // Returns true if the form submitted is valid, false if not. function validate_form($validations_array) { $validate_form = true; foreach ($validations_array as $key => $value) { if (is_array(ABCTomM8::get_query_string_value($key))) { $index = 0; foreach (ABCTomM8::get_query_string_value($key) as $sub_value) { if (ABCTomM8::validate_value($value, $sub_value, $key."_".$index."_error") == false) { $validate_form = false; } $index++; } } else { if (preg_match("/required/i", $value) && isset($_POST[$key."_0"])) { if (is_array(ABCTomM8::get_query_string_value($key."_0"))) { // For checkbox fields. $index = 0; foreach ($_POST["validation_0"] as $row) { # code... $i = 0; $data = ""; do { $data .= $_POST[$key."_".$i][$index]; if ($data != "") { $data .= " "; } $i++; } while (isset($_POST[$key."_".$i][$index])); if (ABCTomM8::validate_value($value, $data, $key."_".$index."_error") == false) { echo $index; $validate_form = false; } $index++; } } else { // For other fields like text, textarea, etc. $i = 0; $data = ""; do { $data .= $_POST[$key."_".$i]; if ($data != "") { $data .= " "; } $i++; } while (isset($_POST[$key."_".$i])); if (ABCTomM8::validate_value($value, $data, $key."_error") == false) { $validate_form = false; } } } else { if (ABCTomM8::validate_value($value, ABCTomM8::get_query_string_value($key), $key."_error") == false) { $validate_form = false; } } } } return $validate_form; } function check_captcha($captcha_field_name) { $securimage = new Securimage(); if ($securimage->check($_POST[$captcha_field_name]) == false) { $_SESSION[$captcha_field_name."_error"] = "invalid captcha code, try again!"; return false; } else { return true; } } // Adds a form field to the page. function add_form_field($instance, $field_type, $field_label, $field_id, $field_name, $field_attributes = array(), $container_element, $container_attributes = array(), $value_options = array(), $field_index = -1) { $field_content = ""; foreach ($field_attributes as $key => $value) { $field_content .= "$key='$value' "; } $container_content = ""; foreach ($container_attributes as $key => $value) { $container_content .= "$key='$value' "; } if ($instance == null && preg_match("/^tomm8te_admin_option::/", $field_name)) { $field_name = str_replace("tomm8te_admin_option::", "", $field_name); $field_value = get_option($field_name); if (count($_POST) > 0) { if ($field_index >= 0) { $field_value = ABCTomM8::get_query_string_value($field_name, $field_index); } else { $field_value = ABCTomM8::get_query_string_value($field_name); } } } else { $field_value = $instance->$field_name; if ($instance == null || count($_POST) > 0) { if ($field_index >= 0) { $field_value = ABCTomM8::get_query_string_value($field_name, $field_index); } else { $field_value = ABCTomM8::get_query_string_value($field_name); } } } $field_id_with_without_index = $field_id; $field_name_with_without_array = $field_name; $field_checkbox_array = ""; if ($field_index >= 0) { $field_checkbox_array = "[".$field_index."]"; $field_name_with_without_array .= "[]"; $field_id_with_without_index .= "_".$field_index; } $field_type = strtolower($field_type); if (!is_array($field_value)) { $field_value = str_replace("&", "&", htmlentities(htmlentities($field_value, ENT_NOQUOTES), ENT_QUOTES)); } if ($field_type != "hidden") { echo("<$container_element $container_content>"); if ($field_label != "") { if ($field_type == "checkbox") { echo(""); } else if ($field_type == "placeholder_text" || $field_type == "placeholder_textarea") { // Do nothing } else { echo(""); } } } if ($field_type == "text") { echo(""); } else if ($field_type == "hidden") { echo(""); } else if ($field_type == "placeholder_text") { echo(""); } else if ($field_type == "file") { echo(""); } else if ($field_type == "textarea") { echo(""); } else if ($field_type == "placeholder_textarea") { echo(""); } else if ($field_type == "captcha") { echo(""); echo("[ Different Image ]"); } else if ($field_type == "select") { echo(""); } else if ($field_type == "radio") { echo(""); } else if ($field_type == "checkbox") { echo(""); } if ($field_index >= 0) { $field_id = $field_id."_".$field_index; } if ($_SESSION[$field_id."_error"] != "") { echo "".$_SESSION[$field_id."_error"].""; } unset($_SESSION[$field_id."_error"]); if ($field_type != "hidden") { echo(""); } } // Adds a form field to the page. Only difference is the value is from the Wordpress get_option database table. Example get_option("siteurl"). function add_option_form_field($field_type, $field_label, $field_id, $option_name, $field_attributes = array(), $container_element, $container_attributes = array(), $value_options = array(), $field_index = -1) { ABCTomM8::add_form_field(null, $field_type, $field_label, $field_id, "tomm8te_admin_option::".$option_name, $field_attributes, $container_element, $container_attributes, $value_options, $field_index); } // Creates the option in the database if it doesn't exist. For example: create_option_if_not_exist("plugin_version_no"). function create_option_if_not_exist($option_name) { if (!get_option($option_name)) { add_option($option_name); } } // Creates a MySQL database table. Returns a create table sql query object. function create_table($table_name, $fields_array_with_datatype, $primary_key_array) { global $wpdb; $table_name_prefix = $wpdb->prefix . $table_name; $fields_comma_separated = implode(",", $fields_array_with_datatype); $primary_key_comma_separated = implode(",", $primary_key_array); $primary_key_text = ", PRIMARY KEY ($primary_key_comma_separated)"; if (count($primary_key_array) > 1) { $primary_key_text = ", UNIQUE KEY ".$primary_key_array[0]." ($primary_key_comma_separated)"; } $sql = "CREATE TABLE $table_name_prefix ($fields_comma_separated $primary_key_text);"; return dbDelta($sql); } // Adds fields to a MySQL Database table. Returns a alter table sql query object. function add_fields_to_table($table_name, $fields_array_with_datatype) { global $wpdb; $table_name_prefix = $wpdb->prefix . $table_name; $fields_comma_separated = implode(",", $fields_array_with_datatype); $sql = "ALTER TABLE $table_name_prefix ADD $fields_comma_separated"; return $wpdb->query($sql); } // Run before making inserts and updates and then you can later rollback or commit a transaction. function start_transaction($transaction_id) { global $wpdb; global $wp_transaction_id; if ( !isset($wp_transaction_id) ) { $wp_transaction_id = $transaction_id; $wpdb->query("START TRANSACTION;"); } } // Rollback transaction. function rollback_transaction($transaction_id) { global $wpdb; global $wp_transaction_id; if ( isset($wp_transaction_id) && $wp_transaction_id == $transaction_id ) { unset($wp_transaction_id); $wpdb->query("ROLLBACK;"); } } // Commit a transaction. function commit_transaction($transaction_id) { global $wpdb; global $wp_transaction_id; if ( isset($wp_transaction_id) && $wp_transaction_id == $transaction_id ) { unset($wp_transaction_id); $wpdb->query("COMMIT;"); } } // Inserts data into the database. Returns true if inserted correct, false if not. function insert_record($table_name, $insert_array) { global $wpdb; ob_start(); $wpdb->show_errors(); $table_name_prefix = $wpdb->prefix.$table_name; $result = $wpdb->insert($table_name_prefix, $insert_array); $wpdb->print_error(); $errors = ob_get_contents(); ob_end_clean(); if (preg_match("/WordPress database error:<\/strong> \[\]/", $errors)) { return true; } else { $sql = "SHOW INDEXES FROM $table_name_prefix WHERE non_unique =0 AND Key_name != 'PRIMARY'"; $results = $wpdb->get_results($sql); foreach ($results as $result) { $col_name = $result->Column_name; if (preg_match("/Duplicate entry (.+)'".$col_name."']/", $errors, $matches, PREG_OFFSET_CAPTURE)) { if (!preg_match("/Must have a unique value/", $_SESSION[$col_name."_error"])) { $_SESSION[$col_name."_error"] .= "Must have a unique value."; } } } return false; } } // Updates data in the database. Returns true if updated correctly, false if not. function update_record_by_id($table_name, $update_array, $id_column_name, $id) { global $wpdb; ob_start(); $wpdb->show_errors(); $table_name_prefix = $wpdb->prefix.$table_name; $result = $wpdb->update($table_name_prefix, $update_array, array($id_column_name => $id)); $wpdb->print_error(); $errors = ob_get_contents(); ob_end_clean(); if (preg_match("/WordPress database error:<\/strong> \[\]/", $errors)) { return true; } else { $sql = "SHOW INDEXES FROM $table_name_prefix WHERE non_unique =0 AND Key_name != 'PRIMARY'"; $results = $wpdb->get_results($sql); foreach ($results as $result) { $col_name = $result->Column_name; if (preg_match("/Duplicate entry (.+)'".$col_name."']/", $errors, $matches, PREG_OFFSET_CAPTURE)) { if (!preg_match("/Must have a unique value/", $_SESSION[$col_name."_error"])) { $_SESSION[$col_name."_error"] .= "Must have a unique value."; } } } return false; } } // Similar to update_record_by_id, but you have more control over which record to update. Returns true if updated correctly, false if not. function update_record($table_name, $update_array, $where_array) { global $wpdb; ob_start(); $wpdb->show_errors(); $table_name_prefix = $wpdb->prefix.$table_name; $result = $wpdb->update($table_name_prefix, $update_array, $where_array); $wpdb->print_error(); $errors = ob_get_contents(); ob_end_clean(); if (preg_match("/WordPress database error:<\/strong> \[\]/", $errors)) { return true; } else { $sql = "SHOW INDEXES FROM $table_name_prefix WHERE non_unique =0 AND Key_name != 'PRIMARY'"; $results = $wpdb->get_results($sql); foreach ($results as $result) { $col_name = $result->Column_name; if (preg_match("/Duplicate entry (.+)'".$col_name."']/", $errors, $matches, PREG_OFFSET_CAPTURE)) { if (!preg_match("/Must have a unique value/", $_SESSION[$col_name."_error"])) { $_SESSION[$col_name."_error"] .= "Must have a unique value."; } } } return false; } } // Deletes a record from the database. Returns a sql delete query object. function delete_record_by_id($table_name, $id_column_name, $delete_id) { global $wpdb; $table_name_prefix = $wpdb->prefix.$table_name; return $wpdb->query($wpdb->prepare("DELETE FROM $table_name_prefix WHERE $id_column_name = %d", $delete_id)); } // Similar to delete_record_by_id, but more flexibility with selecting the record that you want to delete. function delete_record($table_name, $where_sql) { global $wpdb; $table_name_prefix = $wpdb->prefix.$table_name; return $wpdb->query("DELETE FROM $table_name_prefix WHERE $where_sql"); } // Get total record count from database table. // $table_name = (string) The name of table you wish to find the record count for, without the prefix. The prefix is auto added in for you. // $where_sql = (string)(optional) The SQL Where clause without the keyword WHERE. function get_record_count($table_name, $where_sql = "") { global $wpdb; $table_name_prefix = $wpdb->prefix.$table_name; if (!empty($where_sql)) { $where_sql = "WHERE ".$where_sql; } $sql = "SELECT COUNT(*) as count FROM $table_name_prefix $where_sql"; // echo $sql; return $wpdb->get_row($sql)->count; } // Select records from the database. Returns sql results object. function get_results($table_name, $fields_array, $where_sql, $order_array = array(), $limit = "") { global $wpdb; $table_name_prefix = $wpdb->prefix.$table_name; if ($fields_array == "*") { $fields_comma_separated = "*"; } else { $fields_comma_separated = implode(",", $fields_array); } if (!empty($where_sql)) { $where_sql = "WHERE ".$where_sql; } $order_sql = ""; if (!empty($order_array)) { $order_sql = "ORDER BY ".implode(",", $order_array); } $limit_sql = ""; if ($limit != "") { $limit_sql = "LIMIT $limit"; } $sql = "SELECT $fields_comma_separated FROM $table_name_prefix $where_sql $order_sql $limit_sql"; // echo $sql; return $wpdb->get_results($sql); } // Selects a record from the database. Returns one sql record result object. function get_row_by_id($table_name, $fields_array, $id_column_name, $id) { global $wpdb; $table_name_prefix = $wpdb->prefix.$table_name; if ($fields_array == "*") { $fields_comma_separated = "*"; } else { $fields_comma_separated = implode(",", $fields_array); } return $wpdb->get_row($wpdb->prepare("SELECT $fields_comma_separated FROM $table_name_prefix WHERE $id_column_name = %d", $id)); } // Similar to get_row_by_id, but more flexibility with selecting the record that you want. function get_row($table_name, $fields_array, $where_sql) { global $wpdb; $table_name_prefix = $wpdb->prefix.$table_name; if ($fields_array == "*") { $fields_comma_separated = "*"; } else { $fields_comma_separated = implode(",", $fields_array); } return $wpdb->get_row("SELECT $fields_comma_separated FROM $table_name_prefix WHERE $where_sql LIMIT 1"); } } } ?>