Plugin Directory

Changeset 2075888


Ignore:
Timestamp:
04/27/2019 05:25:14 PM (7 years ago)
Author:
koelle
Message:

test

Location:
wpsc-stock-counter
Files:
7 added
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • wpsc-stock-counter/trunk/readme.txt

    r2075870 r2075888  
    11=== WPSC Stock Counter ===
    2 Contributors:
    3 Stable tag: 1.6.3
     2Contributors: Kolja
     3Tags: shopping cart, tickets, shop, e-commerce, stock counter
     4Requires at least: 2.3
     5Tested up to: 4.7.5
     6Stable tag: 1.6.4
    47
    5 This plugin is no longer available
     8Plugin for the [WP E-Commerce Plugin](http://wordpress.org/extend/plugins/wp-e-commerce/) to count stock also of connected products.
     9
     10== Description ==
     11
     12This Plugin is for the [WP E-Commerce Plugin](http://wordpress.org/extend/plugins/wp-e-commerce/) to count the product stock. Different products can be connected via a comma separated list of their IDs to be counted together. Works with WP E-Commerce 3.9.
     13
     14**Translations**
     15
     16* German
     17
     18== Installation ==
     19
     20To install the plugin to the following steps
     21
     221. Unzip the zip-file and upload the content to your Wordpress Plugin directory.
     232. Activiate the plugin via the admin plugin page.
     243. Go to Shopping Cart -> Stock Counter
     25
     26== Frequently Asked Questions ==
     27
     28None so far.
     29
     30== Credits ==
     31The WPSC Stock Counter icon is adapted from the Fugue Icons of http://www.pinvoke.com/.
     32
     33== Screenshots ==
     341. Administration Page
     35
     36== Changelog ==
     37
     38= 1.6.2 =
     39* UPDATED: updated translation template using custom perl script
     40
     41= 1.6.1 =
     42* NEW: mobile responsive admin panel
     43* UPDATE: updated function visibilities
     44* UPDATE: updated translation template
     45
     46= 1.6 =
     47* some small fixes
     48
     49= 1.5.9 =
     50* some small fixes
     51
     52= 1.5.8 =
     53* BUGFIX: fixed some small issues
     54
     55= 1.5.7 =
     56* NEW: give error message if WP eCommerce Plugin is not activated
     57
     58= 1.5.6 =
     59* BUGFIX: use esc_url() on URLs
     60
     61= 1.5.5 =
     62* BUGFIX: small fixes
     63
     64= 1.5.4 =
     65* UPDATE: small design update
     66* BUGFIX: fixed possible undefined index issue
     67
     68= 1.5.3 =
     69* BUGFIX: add stripslashes to product names
     70
     71= 1.5.2 =
     72* BUGFIX: small fixes
     73
     74= 1.5.1 =
     75* BUGFIX: settings not correctly saved
     76* removed Java Script
     77
     78= 1.5 =
     79* NEW: Compatible with Wordpress 4.0.1 and WP E-Commerce 3.9
     80* BUGFIX: escape html specialchars in $_POST variables for security
     81
     82= 1.0 - 1.4 =
     83* First versions for WP E-commerce 3.7.4.
  • wpsc-stock-counter/trunk/wpsc-stock-counter.php

    r2075872 r2075888  
    33Plugin Name: WPSC Stock Counter
    44Plugin URI: http://wordpress.org/extend/plugins/wpsc-stock-counter
    5 Description:
    6 Version: 1.6.3
    7 Author:
     5Description: Plugin for <a href="https://wordpress.org/plugins/wp-e-commerce/">Wordpress Shopping Cart</a> to count product stock. Products can be combined to be counted together.
     6Version: 1.6.4
     7Author: Kolja
    88
    99Copyright 2007-2017
     
    2222along with this program.  If not, see <http://www.gnu.org/licenses/>.
    2323*/
    24 
    25  ?>
     24 
     25/**
     26 * Class implementing stock counter
     27 */
     28class WPSC_StockCounter {
     29    /**
     30     * all products with options
     31     *
     32     * @var array
     33     */
     34    private $products = array();
     35       
     36       
     37    /**
     38     * class constructor. Initialize plugin: define constants, register hooks and actions
     39     *
     40     */
     41    public function __construct() {
     42        if ( !defined( 'WP_CONTENT_URL' ) ) {
     43            /**
     44             * @ignore
     45             */
     46            define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
     47        }
     48       
     49        if ( !defined( 'WP_PLUGIN_URL' ) ) {
     50            /**
     51             * @ignore
     52             */
     53             define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
     54        }
     55       
     56        register_activation_hook(__FILE__, array(&$this, 'activate') );
     57        load_plugin_textdomain( 'wpsc-stock-counter', false, basename(__FILE__, '.php').'/languages' );
     58        add_action( 'admin_menu', array(&$this, 'addAdminMenu') );
     59
     60        // Uninstallation for WP 2.7
     61        if ( function_exists('register_uninstall_hook') )
     62            register_uninstall_hook(__FILE__, array('WPSC_StockCounter', 'uninstall'));
     63           
     64        $this->plugin_url = esc_url(WP_PLUGIN_URL.'/'.basename(__FILE__, '.php'));
     65        $this->getProducts();
     66    }
     67   
     68
     69    /**
     70     * get products list from database
     71     *
     72     * @return boolean
     73     */
     74    private function getProducts() {
     75        global $wpdb;
     76
     77        $products = $wpdb->get_results( "SELECT `ID`, `post_name` FROM {$wpdb->prefix}posts WHERE post_type = 'wpsc-product' ORDER BY id ASC" );
     78        if ( $products ) {
     79            foreach ( $products AS $product ) {
     80                $this->products[$product->ID]['name'] = stripslashes($product->post_name);
     81                $this->getProductMeta( $product->ID ); 
     82            }
     83            return true;
     84        }
     85
     86        return false;
     87    }
     88
     89
     90    /**
     91     * get number of sold objects for given product
     92     *
     93     * @param integer $pid ID of product
     94     * @return integer
     95     */
     96    private function getSoldTickets( $pid ) {
     97        global $wpdb;
     98       
     99        $sold = 0;
     100        $pid = intval($pid);
     101        if ($pid > 0) {
     102            $tickets = $wpdb->get_results( $wpdb->prepare("SELECT `quantity` FROM {$wpdb->prefix}wpsc_cart_contents WHERE `prodid` = '%d'", $pid) );
     103            if ( $tickets ) {
     104                foreach ( $tickets AS $ticket )
     105                    $sold += $ticket->quantity;
     106            }
     107            if ( $this->products[$pid]['linked_products'] != '' ) {
     108                $linked_products = explode( ',', $this->products[$pid]['linked_products'] );
     109                foreach ( $linked_products AS $l_pid ) {
     110                    $l_pid = intval($l_pid);
     111                    if ($l_pid > 0) {
     112                        $tickets = $wpdb->get_results( $wpdb->prepare("SELECT `quantity` FROM {$wpdb->prefix}wpsc_cart_contents WHERE `prodid` = '%d'", $l_pid) );
     113                        if ( $tickets ) {
     114                            foreach ( $tickets AS $ticket ) {
     115                                $sold += $ticket->quantity;
     116                            }
     117                        }
     118                    }
     119                }
     120            }
     121        }
     122        return $sold;
     123    }
     124
     125       
     126    /**
     127     * get product data for given product
     128     *
     129     * @param integer $pid ID of product
     130     */
     131    private function getProductMeta( $pid ) {
     132        $pid = intval($pid);
     133        $options = get_option( 'wpsc-stock-counter' );
     134       
     135        if ( !isset($options['products']) ) {
     136            $options['products'][$pid] = array( 'limit' => 0, 'count' => 0, 'linked_products' => '');
     137        }
     138       
     139        $this->products[$pid]['limit'] = intval($options['products'][$pid]['limit']);
     140        $this->products[$pid]['count'] = intval($options['products'][$pid]['count']);
     141        $this->products[$pid]['linked_products'] = $options['products'][$pid]['linked_products'];
     142
     143        if ( 1 == $this->products[$pid]['count'] ) {
     144            $sold = $this->getSoldTickets( $pid );
     145            $this->products[$pid]['sold'] = $sold;
     146            $this->products[$pid]['remaining'] = $this->products[$pid]['limit'] - $sold;
     147        }
     148    }
     149
     150
     151    /**
     152     * print administration page
     153     *
     154     */
     155    public function printAdminPage() {     
     156        if ( isset( $_POST['updateProductsCounter'] ) && current_user_can('edit_stock_counter_settings') ) {
     157            check_admin_referer( 'wpsc-stock-counter-update-settings_stock' );
     158
     159            $options = get_option( 'wpsc-stock-counter' );
     160            foreach ( $_POST['products'] AS $pid => $data ) {
     161                $pid = intval($pid);
     162                if (!isset($data['count'])) $data['count'] = 0;
     163                foreach($data as $i => $tmp) {
     164                    // escape html characters for security
     165                    if ( $i == 'linked_products' )
     166                        $data2[$i] = htmlspecialchars($tmp);
     167                    else
     168                        $data2[$i] = intval($tmp);
     169                }
     170               
     171                $options['products'][$pid] = $data2;
     172            }
     173            update_option( 'wpsc-stock-counter', $options );
     174           
     175            echo '<div class="updated fade"><p><strong>'.__( 'Settings saved', 'wpsc-stock-counter' ) .'</strong></p></div>';
     176            $this->getProducts();
     177        }
     178        $class = "";
     179       
     180        if ( !$this->wpShoppingCartActive() )
     181                echo '<div class="error"><p><strong>'.__( "WP eCommerce plugin does not seem to be activated.", "wpsc-stock-counter" ).'</strong></p></div>';
     182           
     183?>
     184        <div class="wrap">
     185            <h2><?php _e( 'Stock Summary', 'wpsc-stock-counter' ) ?></h2>
     186            <?php if ( current_user_can('edit_stock_counter_settings') ) : ?>
     187            <div class="tablenav">
     188                <div class="alignleft actions">
     189                    <?php if ( isset($_GET['showsettings']) && $_GET['showsettings'] == 1 ) : ?>
     190                    <p><a class="button button-secondary" href="<?php esc_url(menu_page_url('wpsc-stock-counter')) ?>"><?php _e( 'Hide Settings', 'wpsc-stock-counter' ) ?></a></p>
     191                    <?php else :?>
     192                    <p><a class="button button-secondary" href="<?php esc_url(menu_page_url('wpsc-stock-counter')) ?>&showsettings=1"><?php _e( 'Show Settings', 'wpsc-stock-counter' ) ?></a></p>
     193                    <?php endif; ?>
     194                </div>
     195            </div>
     196            <?php endif; ?>
     197           
     198            <table class="widefat wp-list-table">
     199            <thead>
     200                <tr>
     201                    <th class="manage-column column-product column-primary" scope="col"><?php _e( 'Product', 'wpsc-stock-counter' ) ?></th>
     202                    <th class="manage-column column-sold" scope="col"><?php _e( 'Sold', 'wpsc-stock-counter' ) ?></th>
     203                    <th class="manage-column column-available" scope="col"><?php _e( 'Available', 'wpsc-stock-counter' ) ?></th>
     204                </tr>
     205            </thead>
     206            <tbody>
     207                <?php foreach ( $this->products AS $pid => $data ) : ?>
     208                <?php if ( 1 == $this->products[$pid]['count'] ) : ?>
     209                <?php $class = ( 'alternate' == $class ) ? '' : 'alternate'; ?>
     210                <tr class="<?php echo $class ?>">
     211                    <td class="column-product column-primary" data-colname="<?php _e( 'Product', 'wpsc-stock-counter' ) ?>"><?php echo $data['name'] ?><button class="toggle-row" type="button" /></td>
     212                    <td class="column-sold" data-colname="<?php _e( 'Sold', 'wpsc-stock-counter' ) ?>"><?php echo $this->products[$pid]['sold'] ?></td>
     213                    <td class="column-available" data-colname="<?php _e( 'Available', 'wpsc-stock-counter' ) ?>"><?php echo $this->products[$pid]['remaining'] ?></td>
     214                </tr>
     215                <?php endif; ?>
     216                <?php endforeach; ?>
     217            </tbody>
     218            </table>
     219        </div>
     220        <?php if ( current_user_can('edit_stock_counter_settings') ) : ?>
     221        <?php if (isset($_GET['showsettings'])) : ?>
     222        <div class="wrap" id="wpsc-stock-counter-settings">
     223            <h2><?php _e( 'Settings', 'wpsc-stock-counter' ) ?></h2>
     224           
     225            <form action="<?php esc_url(menu_page_url('wpsc-stock-counter')) ?>&showsettings=1" method="post">
     226                <?php wp_nonce_field( 'wpsc-stock-counter-update-settings_stock' ) ?>
     227               
     228                <table class="widefat wp-list-table">
     229                <thead>
     230                    <tr>
     231                        <th scope="col" class="manage-column column-product column-primary"><?php _e( 'Product', 'wpsc-stock-counter' ) ?></th>
     232                        <th scope="col" class="manage-column column-count"><?php _e( 'Count', 'wpsc-stock-counter' ) ?></th>
     233                        <th scope="col" class="manage-column column-stock"><?php _e( 'Stock', 'wpsc-stock-counter' ) ?></th>
     234                        <th scope="col" class="manage-column column-connected-products"><?php _e( 'Connected Products', 'wpsc-stock-counter' ) ?>*</th>
     235                        <th scope="col" class="manage-column column-ID"><?php _e( 'ID', 'wpsc-stock-counter' ) ?></th>
     236                    </tr>
     237                </thead>
     238                <tbody>
     239                    <?php if (count($this->products)) : ?>
     240                    <?php foreach ( $this->products AS $pid => $data ) : $selected = ( $this->products[$pid]['count'] == 1 ) ? " checked='checked'" : ''; ?>
     241                    <?php $class = ( 'alternate' == $class ) ? '' : 'alternate'; ?>
     242                    <tr class="<?php echo $class ?>">
     243                        <td class="column-product column-primary" data-colname="<?php _e( 'Product', 'wpsc-stock-counter' ) ?>"><?php echo $data['name'] ?><button class="toggle-row" type="button" /></td>
     244                        <td class="column-count" data-colname="<?php _e( 'Count', 'wpsc-stock-counter' ) ?>"><input type="checkbox" name="products[<?php echo $pid ?>][count]"<?php echo $selected ?> value="1" /></td>
     245                        <td class="column-stock" data-colname="<?php _e( 'Stock', 'wpsc-stock-counter' ) ?>"><input type="text" name="products[<?php echo $pid ?>][limit]" value="<?php echo $this->products[$pid]['limit'] ?>" /></td>
     246                        <td class="column-connected-products" data-colname="<?php _e( 'Connected Products', 'wpsc-stock-counter' ) ?>"><input type="text" name="products[<?php echo $pid ?>][linked_products]" value="<?php echo $this->products[$pid]['linked_products'] ?>" /></td>
     247                        <td class="column-ID" data-colname="<?php _e( 'ID', 'wpsc-stock-counter' ) ?>"><?php echo $pid ?></td>
     248                    </tr>
     249                    <?php endforeach; ?>
     250                    <?php endif; ?>
     251                </tbody>
     252                </table>
     253                <p class="tagline-description description">*<?php _e( 'Comma separated list of IDs', 'wpsc-stock-counter' ) ?></p> 
     254   
     255                <p class="submit"><input type="submit" name="updateProductsCounter" value="<?php _e( 'Save Settings', 'wpsc-stock-counter' ) ?>" class="button button-primary" /></p>
     256            </form>
     257        </div>
     258        <?php endif; ?>
     259        <?php endif;
     260    }
     261
     262       
     263    /**
     264     * Activate Plugin
     265     *
     266     */
     267    public function activate() {
     268        $options = array();
     269        add_option( 'wpsc-stock-counter', $options, '', 'yes' );
     270
     271        /*
     272        * Add Capabilities
     273        */
     274        $role = get_role('administrator');
     275        $role->add_cap('view_stock_counter');
     276        $role->add_cap('edit_stock_counter_settings');
     277       
     278        $role = get_role('editor');
     279        $role->add_cap('view_stock_counter');
     280    }
     281   
     282   
     283    /**
     284     * check if shopping cart plugin is active
     285     *
     286     * @return boolean
     287     */
     288    private function wpShoppingCartActive() {
     289        if ( is_plugin_active('wp-e-commerce/wp-shopping-cart.php') )
     290            return true;
     291   
     292        return false;
     293    }
     294    /**
     295     * check if shopping cart plugin is installed
     296     *
     297     * @return boolean
     298     */
     299    private function wpShoppingCartInstalled() {
     300        global $wpdb;
     301       
     302        // check if table chc_config exists
     303        if ( $wpdb->query("SHOW TABLES LIKE '".$wpdb->prefix."wpsc_purchase_logs'") == 1 )
     304            return true;
     305       
     306        return false;
     307    }
     308   
     309
     310    /**
     311     * add admin menu
     312     *
     313     */
     314    public function addAdminMenu() {
     315        $plugin = basename(__FILE__,'.php').'/'.basename(__FILE__);
     316
     317        $mypage = add_options_page( __( 'Shop Stock Counter', 'wpsc-stock-counter' ), __( 'Shop Stock Counter', 'wpsc-stock-counter' ), 'view_stock_counter', 'wpsc-stock-counter', array(&$this, 'printAdminPage') );
     318        add_filter( 'plugin_action_links_' . $plugin, array( &$this, 'pluginActions' ) );
     319    }
     320     
     321     
     322    /**
     323     * display link to settings page in plugin table
     324     *
     325     * @param array $links array of action links
     326     * @return array filtered array plugin actions
     327     */
     328    public function pluginActions( $links ) {
     329        $settings_link = '<a href="'.esc_url(menu_page_url('wpsc-stock-counter', 0)).'">' . __('Settings') . '</a>';
     330        array_unshift( $links, $settings_link );
     331   
     332        return $links;
     333    }
     334   
     335   
     336    /**
     337     * Uninstall Plugin
     338     *
     339     */
     340    public static function uninstall() {
     341        delete_option( 'wpsc-stock-counter' );
     342    }
     343}
     344
     345$wpsc_stock_counter = new WPSC_StockCounter();
     346?>
Note: See TracChangeset for help on using the changeset viewer.