Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Price Update
- Plugin URI: http://mtekk.us/code/
- Description: Updates product prices from a xml file
- Version: 0.0.1
- Author: John Havlik
- Author URI: http://mtekk.us
- */
- /*
- Copyright 2011 John Havlik
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- //Do a PHP version check, require 5.2 or newer
- if(version_compare(phpversion(), '5.2.0', '<'))
- {
- //Only purpose of this function is to echo out the PHP version error
- function bcn_phpold()
- {
- printf('<div class="error"><p>' . __('Your PHP version is too old, please upgrade to a newer version. Your version is %s, Price Update requires %s', 'pr_upate') . '</p></div>', phpversion(), '5.2.0');
- }
- //If we are in the admin, let's print a warning then return
- if(is_admin())
- {
- add_action('admin_notices', 'bcn_phpold');
- }
- return;
- }
- //Include admin base class
- if(!class_exists('mtekk_admin'))
- {
- require_once(dirname(__FILE__) . '/includes/mtekk_admin_class.php');
- }
- /**
- * The administrative interface class
- */
- class PriceUpdate extends mtekk_admin
- {
- protected $version = '0.0.1';
- protected $full_name = 'Price Update';
- protected $short_name = 'Price Update';
- protected $access_level = 'manage_options';
- protected $identifier = 'pr_upate';
- protected $unique_prefix = 'prud';
- protected $plugin_basename = 'price-update/price-update.php';
- protected $opt = array();
- /**
- * __construct()
- *
- * Class default constructor
- */
- function __construct()
- {
- //We set the plugin basename here, could manually set it, but this is for demonstration purposes
- $this->plugin_basename = plugin_dir_path(__FILE__);
- register_deactivation_hook(__FILE__, array($this, 'deactivate'));
- add_action($this->unique_prefix . '_cron_hook', array($this,'cron_handle'));
- //Register some of our custom taxonomies
- add_action('init', array($this, 'wp_init'), 0);
- add_action('wp_footer', array($this, 'footer'));
- //We're going to make sure we load the parent's constructor
- parent::__construct();
- }
- /**
- * admin initialisation callback function
- *
- * is bound to wpordpress action 'admin_init' on instantiation
- *
- * @return void
- */
- function init()
- {
- //We're going to make sure we run the parent's version of this function as well
- parent::init();
- //We can not synchronize our database options untill after the parent init runs (the reset routine must run first if it needs to)
- $this->opt = get_option($this->unique_prefix . '_options');
- //Admin Options update hook
- if(isset($_POST[$this->unique_prefix . '_update_prices']))
- {
- //Do some security related things as we are not using the normal WP settings API
- $this->security();
- // Save Quote Form Options
- $options = $this->get_config();
- $options['quote_enabled'] = ((isset($_POST['quote_enabled']) && $_POST['quote_enabled'] == 'yes') ? true : false);
- $options['quote_form'] = $_POST['quote_form'];
- $options['quote_field'] = $_POST['quote_field'];
- $options['quote_tb_title'] = $_POST['quote_tb_title'];
- $options['quote_tb_width'] = (((int) $_POST['quote_tb_width'] > 0) ? $_POST['quote_tb_width'] : 640);
- $options['quote_tb_height'] = (((int) $_POST['quote_tb_height'] > 0) ? $_POST['quote_tb_height'] : 440);
- update_option('price-update', $options);
- //Do a nonce check, prevent malicious link/form problems
- check_admin_referer($this->unique_prefix . '_update_prices');
- //File opening code
- $file = dirname(__FILE__) . '/' . $_POST[$this->unique_prefix . '_update_prices_file'];
- //Temporarily add update function on init if form has been submitted
- if (file_exists($file) && !is_dir($file))
- $this->update_prices($file);
- }
- if(!wp_next_scheduled($this->unique_prefix . '_cron_hook'))
- {
- wp_schedule_event(time(), 'daily', $this->unique_prefix . '_cron_hook');
- }
- }
- function wp_init()
- {
- register_taxonomy('rsci_id', array('post'), array(
- 'public' => false,
- 'hierarchical' => false,
- 'label' => 'RSCI ID',
- 'query_var' => 'rsci_id',
- 'rewrite' => array('slug' => 'rsci_id')
- ));
- $this->get_config();
- }
- function get_config()
- {
- $options = get_option('price-update');
- if ($options == null)
- $options = array('quote_enabled' => false, 'quote_form' => null, 'quote_field' => 'rscno',
- 'quote_tb_title' => 'Contact for Quote', 'quote_tb_width' => 640, 'quote_tb_height' => 440);
- return $options;
- }
- function footer()
- {
- global $wpdb;
- $options = $this->get_config();
- if ($options['quote_enabled'] != true || (int) $options['quote_form'] <= 0) return false;
- $container = $field = $options['quote_field'];
- $tb_title = htmlspecialchars($options['quote_tb_title'], ENT_QUOTES);
- $tb_width = (int) $options['quote_tb_width'];
- $tb_height = (int) $options['quote_tb_height'];
- $_form = $wpdb->get_var( $wpdb->prepare( "SELECT `display_meta` FROM {$wpdb->prefix}rg_form_meta WHERE `form_id`=%d", $options['quote_form']) );
- if (!empty($_form))
- {
- $_form = @unserialize($_form);
- foreach ($_form['fields'] as $_field)
- {
- if ($_field['inputName'] == $field)
- {
- $field = 'input_'.$_field['id'];
- $container = 'field_'.$options['quote_form'].'_'.$_field['id'];
- break;
- }
- }
- }
- echo <<<HTML
- <script type='text/javascript'>
- function showQuoteForm(value)
- {
- tb_remove();
- jQuery("input[name='{$field}']", "li#{$container}").val(value);
- jQuery("input[name='{$field}']", "li#{$container}").attr('readonly', true);
- tb_show('{$tb_title}', '#TB_inline?width={$tb_width}&height={$tb_height}&modal=false&inlineId=pu_QuoteForm', false);
- return false;
- }
- </script>
- HTML;
- echo '<div id="pu_QuoteForm" style="display: none;">';
- gravity_form($options['quote_form'], true, true, false, null, true);
- echo '</div>';
- gravity_form_enqueue_scripts($options['quote_form'], true);
- }
- function add_page()
- {
- //Add the submenu page to "tools" menu
- $hookname = add_submenu_page('tools.php', __($this->full_name, $this->identifier), $this->short_name, $this->access_level, $this->identifier, array($this, 'admin_page'));
- }
- /**
- * security
- *
- * Makes sure the current user can manage options to proceed
- */
- function security()
- {
- //If the user can not manage options we will die on them
- if(!current_user_can($this->access_level))
- {
- wp_die(__('Insufficient privileges to proceed.', $this->identifier));
- }
- }
- function deactivate()
- {
- wp_clear_scheduled_hook($this->unique_prefix . '_cron_hook');
- }
- //Wrapper to call update_prices
- function cron_handle()
- {
- $file = dirname(__FILE__) . '/cup8100.xml';
- //Generate a file checksum, used to determine if the file was updated
- $file_hash = sha1_file($file);
- //See if we do not have a stored checksum or the hashes do not match
- if(get_transient($this->unique_prefix . '_file_hash') === false || get_transient($this->unique_prefix . '_file_hash') !== $file_hash)
- {
- //Update our transient with the new hash, lasts 10 days
- set_transient($this->unique_prefix . '_file_hash', $file_hash, 60*60*24*10);
- //Parse the file
- $this->update_prices($file);
- }
- }
- /**
- * Upgrades input options array, sets to $this->opt
- *
- * @param array $opts
- * @param string $version the version of the passed in options
- */
- function opts_upgrade($opts, $version)
- {
- }
- function update_prices($file)
- {
- global $wpdb;
- set_time_limit(0);
- remove_action('save_post','yarpp_save_cache');
- if(0 == validate_file($file))
- {
- $start = microtime(true);
- //We want to handle erros
- libxml_use_internal_errors(true);
- $data = implode("", file($file));
- $ItemPriceList = new SimpleXMLElement($data);
- if(!$ItemPriceList)
- {
- $this->message['error'][] = __('Error with XML file.', 'pr_upate');
- foreach(libxml_get_errors() as $error)
- {
- $this->message['error'][] = $error;
- }
- }
- else
- {
- $tobequoted = 0;
- $performed = 0;
- $total = 0;
- $inpost = 0;
- $post_cache = null;
- $prev_id = 0;
- $updated = 0;
- foreach($ItemPriceList->Item as $item)
- {
- $total++;
- $sprice = (string) $item->ItemPrice[0];
- //If we already have a post in the cache, let's see if it has the data we need to modify
- if($post_cache !== null && stripos($post_cache->post_content, 'id="sp'. $item->ItemCode . '"') !== false)
- {
- $query->post = $post_cache;
- }
- //We need to find the post this time
- else
- {
- $args = array(
- 'tax_query' => array(
- array('taxonomy' => 'rsci_id',
- 'terms' => sanitize_title($item->ItemCode),
- 'field' => 'slug')
- ),
- 'posts_per_page' => 1);
- $query = new WP_query;
- $query->query($args);
- }
- if(isset($query->post))
- {
- //If we haven't grabed a post yet, do it
- if($post_cache == null)
- {
- $post_cache = $query->post;
- }
- //If the cache is "stale", store and refresh
- else if($query->post->ID != $post_cache->ID)
- {
- $sstart = microtime(true);
- //Update the post, from cache
- //wp_update_post($post_cache);
- //Yes, I am using a direct query, yes I know it is bad
- $query_str = $wpdb->prepare("UPDATE $wpdb->posts SET post_content = %s WHERE ID = %u AND post_status = 'publish'", array($post_cache->post_content, $post_cache->ID));
- $wpdb->query($query_str);
- $sstop = microtime(true);
- //Replace the cache
- $post_cache = $query->post;
- $updated++;
- if(isset($_POST[$this->unique_prefix . '_update_prices']))
- {
- echo "Updated Post ID: " . $post_cache->ID . " took: " . ($sstop - $sstart) . "sec<br />\n";
- ob_flush();
- }
- }
- $performed++;
- //Assemble our search pattern
- $pattern = '~<td id="sp'. $item->ItemCode . '" class="sale_price">\s*<a ([^>]*)>([^<]*)</a>\s*</td>~i';
- if($item->ItemPrice == 0.0 || $sprice[0] == 'C' || $sprice[0] == 'c')
- {
- $sprice = '<a class="ptb" href="#" onclick="return showQuoteForm(\''.$item->ItemCode.'\');" rel="nofollow">Quote</a>';
- $tobequoted++;
- }
- else
- {
- if($sprice[0] !== 'C' && $sprice[0] !== 'c')
- $sprice = sprintf('\$%.2f', $item->ItemPrice);
- $sprice = '<a href="'.htmlentities('http://shop.rsci.com/industrial_supplies_description.asp?ItemCode='.$item->ItemCode).'" rel="nofollow">'.$sprice.'</a>';
- }
- //Now update the post content
- $post_cache->post_content = preg_replace($pattern, sprintf('<td id="sp%s" class="sale_price">%s</td>', $item->ItemCode, $sprice), $post_cache->post_content);
- if(defined('WP_DEBUG') && WP_DEBUG)
- {
- $this->message['updated fade'][] = sprintf(__('Item %s price updated to %s','pr_upate'), $item->ItemCode, $sprice);
- }
- }
- }
- //Update the post, from cache, must do outside of the loop to capture last changes
- wp_update_post($post_cache);
- $end = microtime(true);
- $this->message['updated fade'][] = 'Processed in: ' . ($end - $start) . 'sec<br />Performed: ' . $performed . ' / To Be Quoted: ' . $tobequoted . ' / Total: ' . $total . '<br />Products Updated: ' . $updated;
- }
- add_action('admin_notices', array($this, 'message'));
- }
- }
- /**
- * admin_page
- *
- * The administrative page for Relatively Perfect
- *
- */
- function admin_page()
- {
- $options = $this->get_config();
- $this->security();
- ?>
- <div class="wrap"><h2><?php _e('Price Update', 'pr_upate'); ?></h2>
- <p<?php if($this->_has_contextual_help): ?> class="hide-if-js"<?php endif; ?>><?php
- print $this->_get_help_text();
- ?></p>
- <form action="tools.php?page=pr_upate" method="post" id="<?php echo $this->unique_prefix;?>_update_prices">
- <?php wp_nonce_field($this->unique_prefix . '_update_prices');?>
- <div id="hasadmintabs">
- <fieldset id="general" class="<?php echo $this->unique_prefix;?>_options">
- <table class="form-table">
- <?php
- //If we don't have titles passed in, we'll use option names as values
- $optid = $this->get_valid_id($this->unique_prefix . '_update_prices_file');?>
- <tr valign="top">
- <th scope="row">
- <label for="<?php echo $optid;?>"><?php _e('Prices File', 'pr_upate');?></label>
- </th>
- <td>
- <select name="<?php echo $this->unique_prefix;?>_update_prices_file" id="<?php echo $optid;?>">
- <option value="">-</option>
- <?php
- //Make sure a directory was passed
- $dir = dirname(__FILE__);
- if(is_dir($dir))
- {
- $contents = array();
- if($dirh = opendir($dir))
- {
- //Makesure the filename isread
- while(false !== ($file = readdir($dirh)))
- {
- //We don't want . or ..'
- if($file != '..' && $file != '.')
- {
- $name = explode('.', $file);
- if(isset($name[1]) && strtolower($name[1]) == 'xml')
- {
- printf('<option value="%s" %s>%s</option>', $file, '', $file);
- }
- }
- }
- //Be good and release some memory
- closedir($dirh);
- }
- }?>
- </select><br />
- <span class="setting-description"><?php printf(__('Select the prices file to process for price updates. Note that the file must be located in the directory "%s" to show up in the dropdown.', 'pr_upate'), $dir);?></span>
- </td>
- </tr>
- </table>
- <h2>Quote Form Options</h2>
- <div><input type="checkbox" name="quote_enabled" value="yes"<?= (($options['quote_enabled'] !== false) ? ' checked="checked"' : null) ?> /> Enabled</div>
- <div>Gravity Form:</div>
- <div> <select name="quote_form"><option value="">-</option><?php
- global $wpdb;
- $_forms = $wpdb->get_results( $wpdb->prepare( "SELECT `id`, `title` FROM {$wpdb->prefix}rg_form WHERE `is_active`=1 ORDER BY `title` ASC") );
- foreach ($_forms as $form)
- echo '<option value="'.$form->id.'"'.(($form->id == $options['quote_form']) ? ' selected' : null).'>'.htmlspecialchars($form->title).'</option>'
- ?></select></div>
- <div>RSC# Field<small> (name of the field with the RSC#)</small>:</div>
- <div> <input type="text" size="80" name="quote_field" value="<?php $str = $options['quote_field']; echo stripslashes($str); ?>" /></div>
- <div>ThickBox Title<small>:</div>
- <div> <input type="text" size="80" name="quote_tb_title" value="<?php $str = $options['quote_tb_title']; echo stripslashes($str); ?>" /></div>
- <div>ThickBox Width<small>:</div>
- <div> <input type="text" size="80" name="quote_tb_width" value="<?php $str = $options['quote_tb_width']; echo stripslashes($str); ?>" /></div>
- <div>ThickBox Height<small>:</div>
- <div> <input type="text" size="80" name="quote_tb_height" value="<?php $str = $options['quote_tb_height']; echo stripslashes($str); ?>" /></div>
- </fieldset>
- </div>
- <p class="submit"><input type="submit" class="button-primary" name="<?php echo $this->unique_prefix;?>_update_prices" value="<?php esc_attr_e('Update'); ?>" /></p>
- </form>
- </div>
- <?php
- }
- }
- //Let's make an instance of our object takes care of everything
- $PriceUpdate = new PriceUpdate;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement