| 1 |
<?php |
| 2 |
/** |
| 3 |
* DataBase Update |
| 4 |
* |
| 5 |
* @package Wow_Plugin |
| 6 |
* @subpackage Includes/Database |
| 7 |
* @author Dmytro Lobov <i@wpbiker.com> |
| 8 |
* @copyright 2019 Wow-Company |
| 9 |
* @license GNU Public License |
| 10 |
* @version 1.0 |
| 11 |
|
| 12 |
*/ |
| 13 |
|
| 14 |
namespace floatingbutton; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Creates and updates a record in the database |
| 22 |
* |
| 23 |
* @since 1.0 |
| 24 |
* |
| 25 |
* @property string plugin_dir - filesystem directory path for the plugin |
| 26 |
* @property string basedir - URL to the file which saving CSS and JS files |
| 27 |
*/ |
| 28 |
class Wow_DB_Update { |
| 29 |
|
| 30 |
/** |
| 31 |
* Setup folder fo saving CSS and JS files |
| 32 |
* |
| 33 |
* @param string $plugin_dir filesystem directory path for the plugin |
| 34 |
* @param string $plugin_slug URL directory path for the plugin |
| 35 |
* |
| 36 |
* @since 1.0 |
| 37 |
*/ |
| 38 |
function __construct( $plugin_dir, $plugin_slug ) { |
| 39 |
$this->plugin['dir'] = $plugin_dir; |
| 40 |
$upload = wp_upload_dir(); |
| 41 |
$this->basedir = $upload['basedir'] . '/' . $plugin_slug . '/'; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Create a new Item in the database |
| 46 |
* |
| 47 |
* @param string $table name of the datatable |
| 48 |
* @param string $info array of parameters to save to the database |
| 49 |
* |
| 50 |
* @since 1.0 |
| 51 |
*/ |
| 52 |
function create_item( $table, $info ) { |
| 53 |
global $wpdb; |
| 54 |
$fields = $wpdb->get_col( "DESC " . $table, 0 ); |
| 55 |
// Collect all passed parameters |
| 56 |
$data = array(); |
| 57 |
foreach ( $fields as $key ) { |
| 58 |
if ( is_array( $info[ $key ] ) == true ) { |
| 59 |
$data[ $key ] = serialize( $info[ $key ] ); |
| 60 |
} else { |
| 61 |
$data[ $key ] = $info[ $key ]; |
| 62 |
} |
| 63 |
} |
| 64 |
// Insert in database |
| 65 |
$wpdb->insert( $table, $data ); |
| 66 |
// Get information for creating JS and CSS files |
| 67 |
$last_id = $wpdb->insert_id; |
| 68 |
$result = $wpdb->get_results( "SELECT * FROM " . $table . " WHERE id = " . $last_id ); |
| 69 |
if ( count( $result ) > 0 ) { |
| 70 |
foreach ( $result as $key => $val ) { |
| 71 |
$param = unserialize( $val->param ); |
| 72 |
// Create the JS file in the plugin folder in uploads |
| 73 |
$file_script = $this->plugin['dir'] . 'admin/partials/generator/script.php'; |
| 74 |
if ( file_exists( $file_script ) ) { |
| 75 |
$path_script = $this->basedir . 'script-' . $last_id . '.js'; |
| 76 |
ob_start(); |
| 77 |
include( $file_script ); |
| 78 |
$content_script = ob_get_contents(); |
| 79 |
ob_end_clean(); |
| 80 |
file_put_contents( $path_script, $content_script ); |
| 81 |
} |
| 82 |
|
| 83 |
// Create the CSS file in the plugin folder in uploads |
| 84 |
$file_style = $this->plugin['dir'] . 'admin/partials/generator/style.php'; |
| 85 |
if ( file_exists( $file_style ) ) { |
| 86 |
$path_style = $this->basedir . 'style-' . $last_id . '.css'; |
| 87 |
ob_start(); |
| 88 |
include( $file_style ); |
| 89 |
$content_style = ob_get_contents(); |
| 90 |
ob_end_clean(); |
| 91 |
file_put_contents( $path_style, $content_style ); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Update an existing item in the database |
| 99 |
* |
| 100 |
* @param string $table name of the datatable |
| 101 |
* @param string $info array of parameters to save to the database |
| 102 |
* |
| 103 |
* @since 1.0 |
| 104 |
*/ |
| 105 |
function update_item( $table, $info ) { |
| 106 |
global $wpdb; |
| 107 |
$fields = $wpdb->get_col( "DESC " . $table, 0 ); |
| 108 |
// Collect all passed parameters |
| 109 |
$data = array(); |
| 110 |
foreach ( $fields as $key ) { |
| 111 |
if ( is_array( $info[ $key ] ) == true ) { |
| 112 |
$data[ $key ] = serialize( $info[ $key ] ); |
| 113 |
} else { |
| 114 |
$data[ $key ] = $info[ $key ]; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$id = absint( $info["id"] ); |
| 119 |
$wpdb->update( $table, $data, array( 'id' => $id ), $format = null, $where_format = null ); |
| 120 |
// Get information for creating JS and CSS files |
| 121 |
$result = $wpdb->get_results( "SELECT * FROM " . $table . " WHERE id = " . $id ); |
| 122 |
if ( count( $result ) > 0 ) { |
| 123 |
foreach ( $result as $key => $val ) { |
| 124 |
$param = unserialize( $val->param ); |
| 125 |
$file_script = $this->plugin['dir'] . 'admin/partials/generator/script.php'; |
| 126 |
if ( file_exists( $file_script ) ) { |
| 127 |
$path_script = $this->basedir . 'script-' . $id . '.js'; |
| 128 |
ob_start(); |
| 129 |
include( $file_script ); |
| 130 |
$content_script = ob_get_contents(); |
| 131 |
ob_end_clean(); |
| 132 |
file_put_contents( $path_script, $content_script ); |
| 133 |
} |
| 134 |
$file_style = $this->plugin['dir'] . 'admin/partials/generator/style.php'; |
| 135 |
if ( file_exists( $file_style ) ) { |
| 136 |
$path_style = $this->basedir . '/style-' . $id . '.css'; |
| 137 |
ob_start(); |
| 138 |
include( $file_style ); |
| 139 |
$content_style = ob_get_contents(); |
| 140 |
ob_end_clean(); |
| 141 |
file_put_contents( $path_style, $content_style ); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|