PluginProbe
Post Snippets – Custom WordPress Code Snippets Customizer / 4.2.2
Post Snippets – Custom WordPress Code Snippets Customizer v4.2.2
4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.1 1.7.2 1.7.3 1.8 1.8.1 1.8.2 1.8.3 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.8.9.1 1.8.9.2 1.9 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 All 115 releases
post-snippets / src / PostSnippets / DBTable.php

DBTable.php in Post Snippets – Custom WordPress Code Snippets Customizer 4.2.2, at src/PostSnippets/DBTable.php

146 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace PostSnippets;
3
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 class DBTable{
7
8 public $psp_db_version = '1.0';
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 PRIMARY KEY (ID)
41 ) $charset_collate;";
42
43
44
45 $sql .= "CREATE TABLE $group_table_name (
46 ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
47 group_name text NOT NULL,
48 group_slug text NOT NULL,
49 group_desc longtext NOT NULL,
50 group_count int unsigned NOT NULL,
51 PRIMARY KEY (ID)
52 ) $charset_collate;";
53
54
55
56 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
57 dbDelta( $sql );
58
59 update_option( 'psp_db_version', $this->psp_db_version );
60 }
61
62 function insert_initial_data(){
63
64 global $wpdb;
65
66 $table_name = $wpdb->prefix . \PostSnippets::GROUP_TABLE_NAME;
67 $ungrouped = __('ungrouped','post-snippets');
68
69 // Properly generate the LIKE query.
70 $like = '%' . $wpdb->esc_like( $ungrouped ) . '%'; // e.g. '%input%'
71 //$like = '%' . $wpdb->esc_like( $myinput ); // e.g. '%input'
72 //$like = $wpdb->esc_like( $myinput ) . '%'; // e.g. 'input%'
73
74 $result = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE group_name LIKE %s ",$like) );
75
76 if(!$result){
77
78 $wpdb->insert(
79 $table_name,
80 array(
81 // 'ID' => $welcome_name,
82 // 'time' => current_time( 'mysql' ),
83 'group_name' => $ungrouped,
84 'group_slug' => $ungrouped,
85 'group_desc' => __('Autogenerated Uncategorized Group', 'post-snippets')
86 )
87 );
88 }
89
90 }
91
92
93
94 public static function update_db_table_previous( $snippets, $old_import = false ){
95
96 global $wpdb;
97
98 $table_name = $wpdb->prefix . \PostSnippets::TABLE_NAME;
99 $table_name_group = $wpdb->prefix . \PostSnippets::GROUP_TABLE_NAME;
100
101 // $wpdb->show_errors();
102 // $wpdb->print_error();
103
104 $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM %1s", $table_name));
105
106 if (!empty($snippets) && $count == 0 || $old_import) {
107
108 $ungrouped = __('ungrouped','post-snippets');
109 $like = '%' . $wpdb->esc_like( $ungrouped ) . '%';
110 $group_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $table_name_group WHERE group_name LIKE %s ",$like) );
111
112 foreach ($snippets as $snippet) {
113
114 $snippet_php = PSallSnippets::get_snippet_php($snippet["php"]);
115
116 // Sanitize snippet content before inserting
117 $snippet_content = Edit::sanitize_snippet_content(
118 $snippet["snippet"] ?? '',
119 $snippet_php
120 );
121
122 $snippet_added = $wpdb->insert(
123 $table_name,
124 array(
125 'snippet_group' => maybe_serialize( array($group_id) ),
126 'snippet_title' => Edit::filter_snippet_title( $snippet["title"] ?? '' ),
127 'snippet_content' => $snippet_content,
128 'snippet_date' => current_time( 'mysql' ),
129 'snippet_vars' => Edit::filter_snippet_vars( $snippet['vars'] ?? '' ),
130 'snippet_desc' => sanitize_textarea_field( $snippet["description"] ?? '' ),
131 'snippet_status' => 1, //Enabled by default
132 'snippet_shortcode' => ( ($snippet["shortcode"] ?? 0 ) == 1 ) ? 1 : 0,
133 'snippet_php' => $snippet_php,
134 'snippet_wptexturize' => ( ($snippet["wptexturize"] ?? 0 ) == 1 ) ? 1 : 0,
135 )
136 );
137
138 if($snippet_added){
139 Edit::change_group_count( array($group_id), 'increment');
140 }
141
142
143 }
144 }
145 }
146 }