Plugin Directory

Changeset 973049


Ignore:
Timestamp:
08/26/2014 08:37:42 AM (12 years ago)
Author:
giuliom
Message:

v 1.1

Location:
mg-donations/trunk
Files:
6 added
4 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • mg-donations/trunk/includes/class-mg-dn-installer.php

    r971226 r973049  
    99        $this->plugin = $mg_dn_plugin;
    1010       
    11         register_activation_hook($this->plugin->path['PLUGIN_FILE'], array($this, 'install'));
     11        register_activation_hook($this->plugin->path['PLUGIN_FILE'], array($this, 'activation'));
    1212    }
    1313   
    14     public function install() {
    15         $this->plugin->settings->setup_factory_settings();
     14    public function activation() {
     15        $this->plugin->settings->setup();
    1616       
    1717        // Refresh permalinks
  • mg-donations/trunk/includes/class-mg-dn-paypal.php

    r971226 r973049  
    77    private $recipient_email;
    88    private $currency;
    9     private $confirm_page;
    10     private $cancel_page;
     9    private $return_url;
     10    private $cancel_url;
    1111    private $return_button_text;
     12    private $return_link_text;
    1213
    1314    public function __construct($cfg) {
     
    1617        $this->recipient_email = $cfg['recipient_email'];
    1718        $this->currency = $cfg['currency'];
    18         $this->confirm_page = $cfg['confirm_page'];
    19         $this->cancel_page = $cfg['cancel_page'];
    20         $this->return_button_text = $cfg['return_button_text'];
     19        $this->return_url = $cfg['return_url'];
     20        $this->cancel_url = $cfg['cancel_url'];
     21        $this->return_link_text = $cfg['return_link_text'];
    2122       
    2223        add_action('mg_dn_ipn', array($this, 'ipn_listener'));
     
    3536            <input type="hidden" name="item_number" value="<?php echo esc_attr($cause_id); ?>">
    3637           
    37             <?php if (!empty($this->confirm_page)): ?>
    38                 <input type="hidden" name="return" value="<?php echo esc_url($this->confirm_page); ?>">
     38            <?php if (!empty($this->return_url)): ?>
     39                <input type="hidden" name="return" value="<?php echo esc_url($this->return_url); ?>">
    3940            <?php endif; ?>
    40             <?php if (!empty($this->cancel_page)): ?>
    41                 <input type="hidden" name="return" value="<?php echo esc_url($this->cancel_page); ?>">
     41            <?php if (!empty($this->cancel_url)): ?>
     42                <input type="hidden" name="cancel_return" value="<?php echo esc_url($this->cancel_url); ?>">
    4243            <?php endif; ?>
    43            
    44             <input type="hidden" name="cbt" value="<?php echo esc_attr($this->return_button_text); ?>">
     44            <?php if (!empty($this->return_link_text)): ?>
     45                <input type="hidden" name="cbt" value="<?php echo esc_attr($this->return_link_text); ?>">
     46            <?php endif; ?>
    4547           
    4648            <?php mg_dn_get_template("buttons/$button_template", $button_args); ?>
  • mg-donations/trunk/includes/class-mg-dn-settings.php

    r971226 r973049  
    33class mg_dn_Settings {
    44
    5     const OPTION_GROUP = 'mg_dn';
    65    const OPTION_NAME = 'mg_dn';
    76
    87    public function __construct() {
    9         add_action('admin_init', array($this, 'setup'));
    10         add_action('admin_notices', array($this, 'user_feedback'));
    118    }
    129   
     
    1512    }
    1613   
    17     public function setup() {
    18         register_setting(self::OPTION_GROUP, self::OPTION_NAME, array($this, 'sanitize'));
    19        
    20         add_settings_section(
    21             'mg_dn_settings_section_general',
    22             'General',
    23             '', // callback
    24             'mg_dn_settings_page'
    25         );
    26        
    27         add_settings_field(
    28             'mg_dn_settings_field_recipient_email',
    29             'PayPal Email',
    30             array($this, 'render_control_recipient_email'),
    31             'mg_dn_settings_page',
    32             'mg_dn_settings_section_general'
    33             //$args
    34         );
    35        
    36         add_settings_field(
    37             'mg_dn_settings_field_currency',
    38             'Currency',
    39             array($this, 'render_control_currency'),
    40             'mg_dn_settings_page',
    41             'mg_dn_settings_section_general'
    42             //$args
    43         );
    44        
    45         add_settings_field(
    46             'mg_dn_settings_field_return_button_text',
    47             'Return Button Text',
    48             array($this, 'render_control_return_button_text'),
    49             'mg_dn_settings_page',
    50             'mg_dn_settings_section_general'
    51             //$args
    52         );
    53        
    54         add_settings_field(
    55             'mg_dn_settings_field_sandbox',
    56             'PayPal Sandbox',
    57             array($this, 'render_control_sandbox'),
    58             'mg_dn_settings_page',
    59             'mg_dn_settings_section_general'
    60             //$args
    61         );
    62        
    63         add_settings_field(
    64             'mg_dn_settings_field_debug',
    65             'Enable Debug',
    66             array($this, 'render_control_debug'),
    67             'mg_dn_settings_page',
    68             'mg_dn_settings_section_general'
    69             //$args
     14    public function get_option_name() {
     15        return self::OPTION_NAME;
     16    }
     17   
     18    public function get_factory_settings() {
     19        return array(
     20            'recipient_email' => '',
     21            'currency' => 'EUR',
     22            'sandbox' => false,
     23            'debug' => false,
     24            'return_url' => '',
     25            'cancel_url' => '',
     26            'return_link_text' => ''
    7027        );
    7128    }
    7229   
    73     public function settings_page() {
    74         $this->settings = $this->get();
    75         ?>
    76         <div class="wrap">
    77             <form action="options.php" method="POST">
    78                 <?php settings_fields(self::OPTION_GROUP); ?>
    79                 <?php do_settings_sections('mg_dn_settings_page');  ?>
    80                 <?php submit_button(__('Save Changes'), 'primary', 'Update'); ?>
    81                 <?php submit_button(__( 'Restore Factory Settings'), 'secondary', 'restore_factory_settings'); ?>
    82             </form>
    83         </div>
    84         <?php
    85     }
    86    
    87     public function get_page() {
    88         return array($this, 'settings_page');
    89     }
    90    
    91     public function render_control_recipient_email() {
    92         ?>
    93         <input type="text" name="<?php echo self::OPTION_NAME . '[recipient_email]'; ?>" value="<?php echo esc_attr($this->settings['recipient_email']); ?>" size="40">
    94     <?php
    95     }
    96    
    97     public function render_control_currency() {
     30    public function sanitize($new_settings) {
     31        $settings = $this->get();
     32       
     33        $settings['sandbox'] = empty($new_settings['sandbox']) ? false : true;
     34        $settings['debug'] = empty($new_settings['debug']) ? false : true;
     35       
     36        if (is_email($new_settings['recipient_email']))
     37            $settings['recipient_email'] = $new_settings['recipient_email'];
     38        else
     39            add_settings_error('mg_dn', 'ko_invalid_email', __('The PayPal email is invalid', 'mg_dn'), 'error');
     40       
    9841        global $mg_dn_plugin;
    99         $currencies = $mg_dn_plugin->money->get_currencies();
    100         $current_currency = $this->settings['currency'];
    101         ?>
    102         <select name="<?php echo self::OPTION_NAME . '[currency]'; ?>">
    103             <?php foreach ($currencies as $code => $info): ?>
    104                 <option value="<?php echo esc_attr($code); ?>"<?php selected($current_currency, $code); ?>><?php echo esc_html($info['name'] . " ({$info['sign']})"); ?></option>
    105             <?php endforeach; ?>
    106         </select>
    107         <?php
    108     }
    109    
    110     public function render_control_return_button_text() {
    111         ?>
    112         <input type="text" name="<?php echo self::OPTION_NAME . '[return_button_text]'; ?>" value="<?php echo esc_attr($this->settings['return_button_text']); ?>" size="40">
    113         <?php
    114     }
    115    
    116     public function render_control_sandbox() {
    117         ?>
    118         <input type="checkbox" name="<?php echo self::OPTION_NAME . '[sandbox]'; ?>" value="yes"<?php checked($this->settings['sandbox'], true); ?>>
    119         <?php
    120     }
    121    
    122     public function render_control_debug() {
    123         ?>
    124         <input type="checkbox" name="<?php echo self::OPTION_NAME . '[debug]'; ?>" value="yes"<?php checked($this->settings['debug'], true); ?>>
    125         <?php
    126     }
    127    
    128     public function sanitize($new_settings) {
    129         if (isset($_POST['restore_factory_settings']))
    130             $settings = $this->get_factory_settings();
    131         else {
    132             $settings = $this->get();
    133            
    134             $settings['sandbox'] = empty($new_settings['sandbox']) ? false : true;
    135             $settings['debug'] = empty($new_settings['debug']) ? false : true;
    136            
    137             if (is_email($new_settings['recipient_email']))
    138                 $settings['recipient_email'] = $new_settings['recipient_email'];
    139             else
    140                 add_settings_error('mg_dn', 'ko_invalid_email', __('The PayPal email is invalid', 'mg_dn'), 'error');
    141            
    142             global $mg_dn_plugin;
    143             if (in_array($new_settings['currency'], array_keys($mg_dn_plugin->money->get_currencies())))
    144                 $settings['currency'] = $new_settings['currency'];
    145             else
    146                 add_settings_error('mg_dn', 'ko_invalid_currency', __('The currency  is invalid', 'mg_dn'), 'error');
    147            
    148             $settings['return_button_text'] = sanitize_text_field($new_settings['return_button_text']);
    149         }
     42        if (in_array($new_settings['currency'], array_keys($mg_dn_plugin->money->get_currencies())))
     43            $settings['currency'] = $new_settings['currency'];
     44        else
     45            add_settings_error('mg_dn', 'ko_invalid_currency', __('The currency  is invalid', 'mg_dn'), 'error');
    15046       
    151         if (count(get_settings_errors('mg_dn')) === 0)
    152             add_settings_error('mg_dn', 'ok', __('Settings saved', 'mg_dn'), 'updated');
     47        $settings['return_url'] = esc_url_raw($new_settings['return_url'], array('http', 'https'));
     48        $settings['cancel_url'] = esc_url_raw($new_settings['cancel_url'], array('http', 'https'));
     49        $settings['return_link_text'] = sanitize_text_field($new_settings['return_link_text']);
    15350       
    15451        return $settings;
    155     }
    156    
    157     private function get_factory_settings() {
    158         return array(
    159             'debug' => false,
    160             'sandbox' => false,
    161             'recipient_email' => '',
    162             'currency' => 'EUR',
    163             'return_button_text' => 'Return to site'
    164             //'confirm_page' => 0, //'http://donations.wp-labs.dev/donation-confirmation-page/',
    165             //'cancel_page' => 0, 'http://donations.wp-labs.dev/donation-cancel-page/',
    166         );
    167     }
    168    
    169     public function setup_factory_settings() {
    170         add_option(self::OPTION_NAME, $this->get_factory_settings());
    17152    }
    17253   
     
    17556    }
    17657   
    177     public function user_feedback() {
    178         settings_errors('mg_dn');
     58    public function setup() {
     59        $factory_settings = $this->get_factory_settings();
     60        $curr_settings = get_option(self::OPTION_NAME);
     61       
     62        if ($curr_settings === false) {
     63            add_option(self::OPTION_NAME, $factory_settings); // This is a fresh install
     64        }
     65        else {
     66            // Remove from the current settings all the key that are not present in factory settings
     67            foreach (array_keys($curr_settings) as $key)
     68                if (!key_exists($key, $factory_settings))
     69                    unset($curr_settings[$key]);
     70                   
     71            // Add to current settings all the new keys in factory settings
     72            foreach (array_keys($factory_settings) as $key)
     73                if (!key_exists($key, $curr_settings))
     74                    $curr_settings[$key] = $factory_settings[$key];
     75           
     76            update_option(self::OPTION_NAME, $curr_settings);
     77        }
    17978    }
    18079   
  • mg-donations/trunk/plugin.php

    r971226 r973049  
    44Plugin URI: http://mgiulio.info/projects/mg-donations
    55Description: Donations
    6 Version: 1.0-beta
     6Version: 1.1
    77Author: Giulio 'mgiulio' Mainardi
    88Author URI: http://mgiulio.info
     
    3535       
    3636        $class_path = $this->path['INCLUDES'];
     37       
    3738        if (strpos($class_name, 'mg_dn_Widget') === 0)
    3839            $class_path .= 'widgets/';
     40        else if (in_array($class_name, array(
     41            'mg_dn_Admin_Menu',
     42            'mg_dn_List_Table_Donation',
     43            'mg_dn_List_Table_Donation_Cause',
     44            'mg_dn_Post_Editor_Donation_Cause',
     45            'mg_dn_Settings_UI',
     46        )))
     47            $class_path .= 'admin/';
     48           
    3949       
    4050        require $class_path . $class_file;
     
    4353    public function bootstrap() {
    4454        $this->settings = new mg_dn_Settings();
    45        
    46         new mg_dn_Admin_Menu();
    4755       
    4856        $this->money = new mg_dn_Money();
     
    6674   
    6775    private function on_admin() {
     76        new mg_dn_Admin_Menu(new mg_dn_Settings_UI($this->settings));
    6877        new mg_dn_List_Table_Donation_Cause();
     78        new mg_dn_List_Table_Donation();
    6979        new mg_dn_Post_Editor_Donation_Cause();
    70         new mg_dn_List_Table_Donation();
    7180       
    7281        add_action('admin_enqueue_scripts', array($this, 'inject_scripts_styles'));
     
    7887        $settings = $this->settings->get();
    7988       
    80         foreach (array('debug', 'sandbox', 'recipient_email', 'currency', /* 'confirm_page', 'cancel_page', */ 'return_button_text') as $key)
    81             $args[$key] = $settings[$key];
    82         $args['confirm_page'] = ''; // get page url
    83         $args['cancel_page'] = ''; // get page url
    84        
    85         $this->paypal = new mg_dn_PayPal($args);
     89        $this->paypal = new mg_dn_PayPal($settings);
    8690       
    8791        add_action('wp_enqueue_scripts', array($this, 'inject_scripts_styles'));
  • mg-donations/trunk/readme.txt

    r971226 r973049  
    2121== Changelog ==
    2222
     23= 1.1 =
     24* Feature: implement PayPal return and cancel urls
     25
    2326= 1.0-beta =
    2427* First release
Note: See TracChangeset for help on using the changeset viewer.