Author: =undo= (info@wpxtre.me)
Inheritance: extends WPDKObject
Example #1
0
 /**
  * Register one or more script following the view controller standard
  *
  * @brief Register
  *
  * @param array $scripts   List of scripts
  *
  *     $scripts = array(
  *         'wpxbz-preferences.js' => array(
  *            'handle'  => 'my-handle',                  // Optional -sanitize_titile( $key ) + remove '.js'
  *            'path'    => WPXBANNERIZE_URL_JAVASCRIPT,  // Optional if set $path params
  *            'deps'    => array( 'jquery' ),            // Optional if set $deps params
  *            'version' => '1.2.3',                      // Optional if set $version
  *            'footer'  => false,                        // Optional override the $in_footer params,
  *          )
  *     );
  *
  *     WPDKScripts::registerScripts( $scripts );
  *
  *     OR
  *
  *     $scripts = array(
  *         'wpxbz-preferences.js',
  *         'wpxbz-admin.js',
  *     );
  *
  *     WPDKScripts::registerScripts( $scripts, $path, $version );
  *
  *     OR
  *
  *     $scripts = array(
  *         'wpxbz-preferences.js' => array(
  *            'handle'  => 'my-handle',                  // Optional -sanitize_titile( $key ) + remove '.js'
  *            'path'    => WPXBANNERIZE_URL_JAVASCRIPT,  // Optional if set $path params
  *            'version' => '1.2.3',                      // Optional if set $version
  *            'footer'  => false,                        // Optional override the $in_footer params
  *          ),
  *         'wpxbz-admin.js',
  *     );
  *
  *     // $path and version will be used for 2th iten
  *     WPDKScripts::registerScripts( $scripts, $path, $version );
  *
  * @param string       $path      Optional. If set all scripts will be loaded fron this url
  * @param string|array $deps      Optional. One or more handle dependiences
  * @param bool|string  $version   Optional. If set will apply to all script
  * @param bool         $in_footer Optional. Default all scripts are loaded in footer
  *
  * @return bool
  */
 public function registerScripts($scripts, $path = '', $deps = array(), $version = false, $in_footer = true)
 {
     if (!empty($scripts) && is_array($scripts)) {
         foreach ($scripts as $filename => $info) {
             // Case 1
             if (is_array($info)) {
                 $handle = isset($info['handle']) ? $info['handle'] : sanitize_title(WPDKFilesystem::filename($filename));
                 $_path = isset($info['path']) ? $info['path'] : $path;
                 $_deps = isset($info['deps']) ? $info['deps'] : $deps;
                 // Sanitize $deps
                 if (is_string($_deps)) {
                     $_deps = explode(',', $_deps);
                 }
                 $_version = isset($info['version']) ? $info['version'] : $version;
                 $_in_footer = isset($info['footer']) ? $info['footer'] : $in_footer;
                 $src = sprintf('%s%s', trailingslashit($_path), $filename);
             } elseif (is_string($info)) {
                 $handle = sanitize_title(WPDKFilesystem::filename($info));
                 $_path = $path;
                 $_deps = $deps;
                 // Sanitize $deps
                 if (is_string($_deps)) {
                     $_deps = explode(',', $_deps);
                 }
                 $_version = $version;
                 $_in_footer = $in_footer;
                 $src = sprintf('%s%s', trailingslashit($_path), $info);
             } else {
                 return false;
             }
             // Stability
             if (!empty($handle) && !empty($src)) {
                 wp_register_script($handle, $src, $_deps, $_version, $_in_footer);
             }
         }
     }
     return true;
 }
Example #2
0
 /**
  * Reads the Database table in $table and creates SQL Statements for recreating structure and data then return the
  * DUMP SQL. If you set the $filename params then the dump is store on filesystem too.
  *
  * Taken partially from phpMyAdmin and partially from Alain Wolf, Zurich - Switzerland
  *
  * Website: http://restkultur.ch/personal/wolf/scripts/db_backup/
  *
  * @brief Dump
  *
  * @param string $table    Table name.
  * @param string $filename Optional. Complete path of a filename where store the dump.
  *
  * @return string
  */
 public function dumpWithTable($table, $filename = '')
 {
     // Prepare dump
     $dump = '';
     // Main information on dump
     $dump .= "# --------------------------------------------------------\n";
     $dump .= "# Date:      " . date('j F, Y H:i:s') . "\n";
     $dump .= "# Database:  " . DB_NAME . "\n";
     $dump .= "# Table:     " . $table . "\n";
     $dump .= "# --------------------------------------------------------\n";
     // Add SQL statement to drop existing table
     $dump .= "\n";
     $dump .= "#\n";
     $dump .= "# Delete any existing table `{$table}`\n";
     $dump .= "#\n";
     $dump .= "\n";
     $dump .= "DROP TABLE IF EXISTS " . self::backquote($table) . ";\n";
     // Comment in SQL-file
     $dump .= "\n";
     $dump .= "#\n";
     $dump .= "# Table structure of table `{$table}`\n";
     $dump .= "#\n";
     $dump .= "\n";
     // Get table structure
     $query = 'SHOW CREATE TABLE ' . self::backquote($table);
     $result = $this->mysqli ? mysqli_query($this->dbh, $query) : mysql_query($query, $this->dbh);
     if ($result) {
         // Get num rows
         $num_rows = $this->mysqli ? mysqli_num_rows($result) : mysql_num_rows($result);
         if ($num_rows > 0) {
             $sql_create_arr = $this->mysqli ? mysqli_fetch_array($result) : mysql_fetch_array($result);
             $dump .= $sql_create_arr[1];
         }
         if ($this->mysqli) {
             mysqli_free_result($result);
         } else {
             mysql_free_result($result);
         }
         $dump .= ' ;';
     }
     // Get table contents
     $query = 'SELECT * FROM ' . self::backquote($table);
     /**
      * Filter the query used to select the rows to dump.
      *
      * The dynamic portion of the hook name, $table, refers to the database table name.
      *
      * @param string $query The SQL query.
      */
     $query = apply_filters('wpdk_db_dump_query-' . $table, $query);
     $result = $this->mysqli ? mysqli_query($this->dbh, $query) : mysql_query($query, $this->dbh);
     $fields_cnt = 0;
     $rows_cnt = 0;
     if ($result) {
         $fields_cnt = $this->mysqli ? mysqli_num_fields($result) : mysql_num_fields($result);
         $rows_cnt = $this->mysqli ? mysqli_num_rows($result) : mysql_num_rows($result);
     }
     // Comment in SQL-file
     $dump .= "\n";
     $dump .= "\n";
     $dump .= "#\n";
     $dump .= "# Data contents of table `{$table}` ({$rows_cnt} records)\n";
     $dump .= "#\n";
     $dump .= "\n";
     // Lock table if insert available
     $dump .= empty($rows_cnt) ? '' : "\nLOCK TABLES `{$table}` WRITE;\n";
     /**
      * Filter the addition SQL comment before printing the INSERT rows.
      *
      * The dynamic portion of the hook name, $table, refers to the database table name.
      *
      * @param string $comment Default empty.
      */
     $dump .= apply_filters('wpdk_db_dump_info_before_inserts-' . $table, '');
     // Checks whether the field is an integer or not
     for ($j = 0; $j < $fields_cnt; $j++) {
         if ($this->mysqli) {
             $object = mysqli_fetch_field_direct($result, $j);
             $field_set[$j] = $object->name;
             $type = $object->type;
         } else {
             $field_set[$j] = self::backquote(mysql_field_name($result, $j));
             $type = mysql_field_type($result, $j);
         }
         // Is number?
         $field_num[$j] = in_array($type, array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'));
     }
     // Sets the scheme
     $entries = 'INSERT INTO ' . self::backquote($table) . ' VALUES (';
     $search = array('\\x00', '\\x0a', '\\x0d', '\\x1a');
     //\x08\\x09, not required
     $replace = array('\\0', '\\n', '\\r', '\\Z');
     $current_row = 0;
     $batch_write = 0;
     while ($row = $this->mysqli ? mysqli_fetch_row($result) : mysql_fetch_row($result)) {
         $current_row++;
         // build the statement
         for ($j = 0; $j < $fields_cnt; $j++) {
             if (!isset($row[$j])) {
                 $values[] = 'NULL';
             } elseif ($row[$j] === '0' || $row[$j] !== '') {
                 // a number
                 if ($field_num[$j]) {
                     $values[] = $row[$j];
                 } else {
                     $values[] = "'" . str_replace($search, $replace, self::addslashes($row[$j])) . "'";
                 }
             } else {
                 $values[] = "''";
             }
         }
         $dump .= "\n" . $entries . implode(', ', $values) . ") ;";
         // write the rows in batches of 100
         if ($batch_write === self::DUMP_SQL_FILE_CHUNCK_SIZE) {
             $batch_write = 0;
             // Write on disk
             if (!empty($filename)) {
                 $result = WPDKFilesystem::append($dump, $filename);
                 // TODO Fires an error or filters to stop the execution
                 $dump = '';
             }
         }
         $batch_write++;
         unset($values);
     }
     if ($this->mysqli) {
         mysqli_free_result($result);
     } else {
         mysql_free_result($result);
     }
     // Unlock tables
     $dump .= empty($rows_cnt) ? '' : "\n\nUNLOCK TABLES;\n";
     // Create footer/closing comment in SQL-file
     $dump .= "\n";
     $dump .= "\n";
     $dump .= "#\n";
     $dump .= "# End of data contents of table " . $table . "\n";
     $dump .= "# --------------------------------------------------------\n";
     $dump .= "\n";
     $dump .= "\n";
     // Write on disk
     if (!empty($filename)) {
         $result = WPDKFilesystem::append($dump, $filename);
         // TODO Fires an error
     }
     return $dump;
 }