| 1 |
<?php |
| 2 |
namespace PostSnippets; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
class DBTable{ |
| 7 |
|
| 8 |
public $psp_db_version = '1.1'; |
| 9 |
|
| 10 |
function __construct() { |
| 11 |
|
| 12 |
$this->create_db_table(); |
| 13 |
$this->insert_initial_data(); |
| 14 |
|
| 15 |
self::update_db_table_previous( get_option(\PostSnippets::OPTION_KEY) ); /**Update table with previous version snippets, for backward compatibility */ |
| 16 |
|
| 17 |
} |
| 18 |
|
| 19 |
function create_db_table(){ |
| 20 |
|
| 21 |
global $wpdb; |
| 22 |
|
| 23 |
$snippets_table_name = $wpdb->prefix . \PostSnippets::TABLE_NAME; |
| 24 |
$group_table_name = $wpdb->prefix . \PostSnippets::GROUP_TABLE_NAME; |
| 25 |
|
| 26 |
$charset_collate = $wpdb->get_charset_collate(); |
| 27 |
|
| 28 |
$sql = "CREATE TABLE $snippets_table_name ( |
| 29 |
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 30 |
snippet_group longtext NOT NULL, |
| 31 |
snippet_title text NOT NULL, |
| 32 |
snippet_content longtext NOT NULL, |
| 33 |
snippet_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, |
| 34 |
snippet_vars longtext NOT NULL, |
| 35 |
snippet_desc longtext NOT NULL, |
| 36 |
snippet_status smallint(11) unsigned NOT NULL, |
| 37 |
snippet_shortcode smallint(11) unsigned NOT NULL, |
| 38 |
snippet_php smallint(11) unsigned NOT NULL, |
| 39 |
snippet_wptexturize smallint(11) unsigned NOT NULL, |
| 40 |
snippet_rawhtml smallint(11) unsigned NOT NULL DEFAULT 1, |
| 41 |
PRIMARY KEY (ID) |
| 42 |
) $charset_collate;"; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
$sql .= "CREATE TABLE $group_table_name ( |
| 47 |
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 48 |
group_name text NOT NULL, |
| 49 |
group_slug text NOT NULL, |
| 50 |
group_desc longtext NOT NULL, |
| 51 |
group_count int unsigned NOT NULL, |
| 52 |
PRIMARY KEY (ID) |
| 53 |
) $charset_collate;"; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 58 |
dbDelta( $sql ); |
| 59 |
|
| 60 |
update_option( 'psp_db_version', $this->psp_db_version ); |
| 61 |
} |
| 62 |
|
| 63 |
function insert_initial_data(){ |
| 64 |
|
| 65 |
global $wpdb; |
| 66 |
|
| 67 |
$table_name = $wpdb->prefix . \PostSnippets::GROUP_TABLE_NAME; |
| 68 |
$ungrouped = __('ungrouped','post-snippets'); |
| 69 |
|
| 70 |
// Properly generate the LIKE query. |
| 71 |
$like = '%' . $wpdb->esc_like( $ungrouped ) . '%'; // e.g. '%input%' |
| 72 |
//$like = '%' . $wpdb->esc_like( $myinput ); // e.g. '%input' |
| 73 |
//$like = $wpdb->esc_like( $myinput ) . '%'; // e.g. 'input%' |
| 74 |
|
| 75 |
$result = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE group_name LIKE %s ",$like) ); |
| 76 |
|
| 77 |
if(!$result){ |
| 78 |
|
| 79 |
$wpdb->insert( |
| 80 |
$table_name, |
| 81 |
array( |
| 82 |
// 'ID' => $welcome_name, |
| 83 |
// 'time' => current_time( 'mysql' ), |
| 84 |
'group_name' => $ungrouped, |
| 85 |
'group_slug' => $ungrouped, |
| 86 |
'group_desc' => __('Autogenerated Uncategorized Group', 'post-snippets') |
| 87 |
) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static function update_db_table_previous( $snippets, $old_import = false ){ |
| 96 |
|
| 97 |
global $wpdb; |
| 98 |
|
| 99 |
$table_name = $wpdb->prefix . \PostSnippets::TABLE_NAME; |
| 100 |
$table_name_group = $wpdb->prefix . \PostSnippets::GROUP_TABLE_NAME; |
| 101 |
|
| 102 |
// $wpdb->show_errors(); |
| 103 |
// $wpdb->print_error(); |
| 104 |
|
| 105 |
$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM %1s", $table_name)); |
| 106 |
|
| 107 |
if (!empty($snippets) && $count == 0 || $old_import) { |
| 108 |
|
| 109 |
$ungrouped = __('ungrouped','post-snippets'); |
| 110 |
$like = '%' . $wpdb->esc_like( $ungrouped ) . '%'; |
| 111 |
$group_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $table_name_group WHERE group_name LIKE %s ",$like) ); |
| 112 |
|
| 113 |
foreach ($snippets as $snippet) { |
| 114 |
|
| 115 |
$snippet_php = PSallSnippets::get_snippet_php($snippet["php"]); |
| 116 |
|
| 117 |
// Sanitize snippet content before inserting |
| 118 |
$snippet_content = Edit::sanitize_snippet_content( |
| 119 |
$snippet["snippet"] ?? '', |
| 120 |
$snippet_php |
| 121 |
); |
| 122 |
|
| 123 |
$snippet_added = $wpdb->insert( |
| 124 |
$table_name, |
| 125 |
array( |
| 126 |
'snippet_group' => maybe_serialize( array($group_id) ), |
| 127 |
'snippet_title' => Edit::filter_snippet_title( $snippet["title"] ?? '' ), |
| 128 |
'snippet_content' => $snippet_content, |
| 129 |
'snippet_date' => current_time( 'mysql' ), |
| 130 |
'snippet_vars' => Edit::filter_snippet_vars( $snippet['vars'] ?? '' ), |
| 131 |
'snippet_desc' => sanitize_textarea_field( $snippet["description"] ?? '' ), |
| 132 |
'snippet_status' => 1, //Enabled by default |
| 133 |
'snippet_shortcode' => ( ($snippet["shortcode"] ?? 0 ) == 1 ) ? 1 : 0, |
| 134 |
'snippet_php' => $snippet_php, |
| 135 |
'snippet_wptexturize' => ( ($snippet["wptexturize"] ?? 0 ) == 1 ) ? 1 : 0, |
| 136 |
'snippet_rawhtml' => 1, |
| 137 |
) |
| 138 |
); |
| 139 |
|
| 140 |
if($snippet_added){ |
| 141 |
Edit::change_group_count( array($group_id), 'increment'); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |