🔥 HOT: Changeset/ - High Quality

Changeset 884496


Ignore:
Timestamp:
03/30/2014 06:15:04 AM (12 years ago)
Author:
aniketpant
Message:

Update plugin to fix older issues

Location:
instamojo/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • instamojo/trunk/instamojo-button.php

    r883569 r884496  
    3737{
    3838  include_once(PLUGIN_DIR.'widget.php');
    39   register_widget('instamojo_widget');
     39  register_widget('Instamojo_Widget');
    4040}
    4141
  • instamojo/trunk/option.php

    r883568 r884496  
    11<?php
    22
    3 define('ENCRYPTION_KEY', '!@#$%^&*');
    4 define('APPLICATION_ID', 'acd73b5ac8ccd76be2dafc46e082d415');
    5 
     3include_once(__DIR__.'/constants.php');
    64include_once(__DIR__.'/lib/Instamojo.php');
    75
     
    97 * Instamojo Settings Page
    108 */
    11 class InstamojoSettingsPage
     9class Instamojo_Settings_Page
    1210{
    1311  private $_options;
     
    1715    add_action('admin_menu', array($this, 'add_plugin_page'));
    1816    add_action('admin_init', array($this, 'page_init'));
    19     add_action('updated_option', array($this, 'auth_token'));
    2017  }
    2118
     
    2623      'Instamojo',
    2724      'manage_options',
    28       'instamojo-admin',
     25      'instamojo',
    2926      array($this, 'create_admin_page')
    3027    );
     
    3431  {
    3532    $this->_options = get_option('instamojo_credentials');
    36     if (isset($this->_options['auth_token']))
     33    $auth_token = $this->_options['auth_token'];
     34
     35    if (isset($_POST['submit']))
     36    {
     37      if (function_exists('current_user_can') && !current_user_can('manage_options'))
     38      {
     39        die(__('Cheatin&#8217; uh?'));
     40      }
     41
     42      $instamojo_credentials = $_POST['instamojo_credentials'];
     43      if (isset($instamojo_credentials))
     44      {
     45        if (isset($auth_token))
     46        {
     47          $this->revoke_token($auth_token);
     48        }
     49        $instance = new Instamojo(APPLICATION_ID, $instamojo_credentials['username'], $instamojo_credentials['password']);
     50        $auth = $instance->apiAuth();
     51        $instamojo_credentials['auth_token'] = $auth['token'];
     52        unset($instamojo_credentials['password']);
     53        update_option('instamojo_credentials', $instamojo_credentials);
     54      }
     55    }
     56
     57    if (isset($_POST['revoke']))
     58    {
     59      if (isset($auth_token))
     60      {
     61        $this->revoke_token($auth_token);
     62      }
     63    }
     64
     65    if ($auth_token)
    3766    {
    3867      echo '<div class="update-nag"><p>You have already authenticated your account with us. If you wish to switch accounts then enter your details again.</p></div>';
     
    4069    ?>
    4170    <div class="wrap">
    42       <h2>Instamojo Options</h2>
    43       <form method="post" action="options.php">
    44       <?php settings_fields('instamojo_credentials-group'); ?>
    45       <?php do_settings_sections('instamojo-admin'); ?>
    46       <?php submit_button('Authenticate'); ?>
     71      <h2><?php _e('Instamojo Options'); ?></h2>
     72
     73      <h3><?php _e('Instamojo Credentials'); ?></h3>
     74      <form method="post" action="" id="instamojo-conf">
     75        <table class="form-table">
     76          <tbody>
     77            <tr>
     78              <th>
     79                <label for="instamojo-username"><?php _e('Username'); ?></label>
     80              </th>
     81              <td>
     82                <input type="text" id="instamojo-username" name="instamojo_credentials[username]" />
     83              </td>
     84            </tr>
     85            <tr>
     86              <th>
     87                <label for="instamojo-password"><?php _e('Password'); ?></label>
     88              </th>
     89              <td>
     90                <input type="password" id="instamojo-password" name="instamojo_credentials[password]" />
     91              </td>
     92            </tr>
     93          </tbody>
     94        </table>
     95        <p class="submit">
     96          <input type="submit" name="submit" id="submit" class="button button-primary<?php if ($this->_options['auth_token']) echo '-disabled'; ?>" value="<?php _e('Authenticate'); ?>" />
     97        </p>
     98      </form>
     99
     100      <h3><?php _e('Revoke Your Authentication Token'); ?></h3>
     101      <form method="post" action="" id="instamojo-token-revoke">
     102        <p class="submit">
     103          <input type="submit" name="revoke" id="revoke-button" class="button button-secondary" value="<?php _e('Revoke Token'); ?>" />
     104        </p>
    47105      </form>
    48106    </div>
     
    54112    register_setting(
    55113      'instamojo_credentials-group',
    56       'instamojo_credentials',
    57       array($this, 'sanitize')
     114      'instamojo_credentials'
    58115    );
    59 
    60     add_settings_section(
    61       'credentials',
    62       'Instamojo Credentials',
    63       array($this, 'print_section_info'),
    64       'instamojo-admin'
    65     );
    66 
    67     add_settings_field(
    68       'instamojo-username',
    69       'Username',
    70       array($this, 'username_callback'),
    71       'instamojo-admin',
    72       'credentials'
    73     );
    74 
    75     add_settings_field(
    76       'instamojo-password',
    77       'Password',
    78       array($this, 'password_callback'),
    79       'instamojo-admin',
    80       'credentials'
    81     );
    82   }
    83 
    84 
    85   /**
    86    * Sanitize each setting field as needed
    87    *
    88    * @param array $input Contains all settings fields as array keys
    89    */
    90   public function sanitize($input)
    91   {
    92     $new_input = array();
    93 
    94     if(isset($input['username']))
    95     {
    96       $new_input['username'] = sanitize_text_field($input['username']);
    97     }
    98 
    99     if(isset($input['password']))
    100     {
    101       $new_input['password'] = $input['password'];
    102     }
    103 
    104     return $new_input;
    105   }
    106 
    107   /**
    108    * Print the Section text
    109    */
    110   public function print_section_info()
    111   {
    112     echo '<p>Enter your login credentials below:</p>';
    113116  }
    114117
     
    123126  }
    124127
    125   public function auth_token()
     128  private function revoke_token($auth_token)
    126129  {
    127     $options = get_option('instamojo_credentials');
    128     if (!$options['auth_token'])
    129     {
    130       $instance = new Instamojo($options['username'], $options['password'], APPLICATION_ID);
    131       $auth = $instance->apiAuth();
    132       $options['auth_token'] = $auth['token'];
    133       unset($options['username']);
    134       unset($options['password']);
    135       update_option('instamojo_credentials', $options);
    136       return $this;
    137     }
     130    $instance = new Instamojo(APPLICATION_ID);
     131    $instance->setAuthToken($auth_token);
     132    $instance->deleteAuthToken();
     133    delete_option('instamojo_credentials');
     134    unset($instance);
    138135  }
    139 
    140136}
    141137
    142138if (is_admin())
    143139{
    144   $my_settings_page = new InstamojoSettingsPage();
     140  $my_settings_page = new Instamojo_Settings_Page();
    145141}
    146142
  • instamojo/trunk/widget.php

    r883564 r884496  
    11<?php
    22
     3include_once(__DIR__.'/constants.php');
     4include_once(__DIR__.'/lib/Instamojo.php');
     5
    36/**
    4  * Instamojo Widget.
    5  * It extends the WordPress Widget class.
     7 * Instamojo Widget
     8 * It extends the WordPress Widget class
    69 */
    7 class instamojo_widget extends WP_Widget
     10class Instamojo_Widget extends WP_Widget
    811{
    912
    10 
    1113  /**
    12    *  Default constructor.
     14   *  Default constructor
    1315   */
    14   function instamojo_widget()
     16  function __construct()
    1517  {
    1618    // Load any other optional scripts
    1719    add_action('load-widgets.php', array(&$this, 'my_custom_load'));
    1820
    19     // Name and class of widget.
     21    // Name and class of widget
    2022    $widget_options = array(
    2123      'classname' => 'instamojo-widget',
    2224      'description' => 'Display Instamojo offers in your blog.');
    2325
    24     // Id, width and height of the widget.
     26    // Id, width and height of the widget
    2527    $control_options = array(
    2628      'id_base' => 'instamojo-widget',
     
    2931
    3032    // Initialize the widget.
    31     $this->WP_Widget('instamojo-widget', 'instamojo',  $widget_options, $control_options);
     33    $this->WP_Widget('instamojo-widget', 'Instamojo',  $widget_options, $control_options);
    3234  }
    3335
     
    4143
    4244  /**
    43    *  Implements the widget() function as required by WordPress.
    44    *  This is responsible for how the widget looks in your WordPress site.
     45   *  Implements the widget() function as required by WordPress
     46   *  This is responsible for how the widget looks in your WordPress site
    4547   */
    4648  function widget($args, $instance)
     
    4951    wp_enqueue_style('widgetcss');
    5052
    51     // Holds a mapping for currency to HTML of that currency.
    52     $currency_html_map = array();
    53     $currency_html_map["INR"] = "&#8377;";
    54     $currency_html_map["USD"] = "&#36;";
     53    $instamojo_credentials = get_option('instamojo_credentials');
    5554
    5655    extract($args);
     56    if (!isset($instance['title']))
     57    {
     58      $instance['title'] = '';
     59    }
    5760    $title = apply_filters('widget_title', $instance['title']);
    5861    echo $before_widget;
    59     $add_feed = (substr($instance['instamojo_url'], -1) == '/') ? "feed.json" : "/feed.json";
    6062
    61     // Getting details from the link given in instamojo_url.
    62     $offer_array = json_decode(file_get_contents($instance['instamojo_url'].$add_feed));
    63 
    64     $offer_title = $offer_array->{'offer'}->{'title'};
    65     $offer_description = $offer_array->{'offer'}->{'description'};
    66     $offer_base_price = $offer_array->{'offer'}->{'base_price'};
    67     $offer_currency = $offer_array->{'offer'}->{'currency'};
    68     $offer_image = $offer_array->{'offer'}->{'cover_image'};
    69 
    70     // If title is not given make it My Instamojo Product.
     63    // If title is not given make it My Instamojo Product
    7164    if ($instance['title']) {
    7265      echo $before_title.$instance['title'].$after_title;
     
    7669    }
    7770
    78     $button_html = '<div class="btn-container"><a href="'.$instance['instamojo_url'].'" ';
     71    $button_html = '<div class="btn-container"><a href="https://www.instamojo.com/'.$instamojo_credentials['username'].'/'.$instance['instamojo_offer'].'" ';
    7972    if ($instance['button_style'] != 'none') {
    8073      $button_html .= 'class="im-checkout-btn btn--'.$instance['button_style'].'" ';
    8174    }
    8275    $button_html .= 'target="_blank">Buy Now</a></div>';
    83     ?>
    84     <div id="wid-small-div">
    85       <?php if ($instance['button_pos'] == 'top') echo $button_html; ?>
    86       <div id="wid-offer-title">
    87         <?php if ($instance['title'] == '404 error') echo '<h4>Error in offer URL!</h4>'; else echo '<h4>'.$offer_title.'</h4>'; ?>
    88       </div>
    89       <div id="wid-currency-price">
    90         <h4><?php echo $currency_html_map[$offer_currency].' '.$offer_base_price; ?></h4>
    91       </div>
    92       <?php if ($instance['button_pos'] == 'bottom') echo $button_html; ?>
    93     </div>
    94     <?php
     76    echo $button_html;
    9577    echo $after_widget;
    9678  }
    9779
    9880  /**
    99    *  Implements the update() function as required by WordPress.
    100    *  This works when you fill data in the widget form input from the WordPress admin.
     81   *  Implements the update() function as required by WordPress
     82   *  This works when you fill data in the widget form input from the WordPress admin
    10183   */
    10284  function update($new_instance, $old_instance)
    10385  {
    10486    $instance = $old_instance;
    105     $instance['button_pos'] = strip_tags($new_instance['button_pos']);
    106     $instance['instamojo_url'] = strip_tags($new_instance['instamojo_url']);
    107     $ch = curl_init($instance['instamojo_url']);
    108     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    109     curl_exec($ch);
    110     $instance['instamojo_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    111     curl_close($ch);
    112     if(substr($instance['instamojo_url'], -1) == '/')
    113       $instance['instamojo_url'] = substr($instance['instamojo_url'], 0, -1);
    114     if($instance['instamojo_url'][0] == 'h' && $instance['instamojo_url'][5] == 's')
    115       $instance['instamojo_url'] = substr($instance['instamojo_url'], 9);
    116     if($instance['instamojo_url'][0] == 'h')
    117       $instance['instamojo_url'] = substr($instance['instamojo_url'], 8);
    118     $instance['title'] = strip_tags($new_instance['title']);
    119     $instance['button_style'] = strip_tags($new_instance['button_style']);
    120     $response = get_headers($instance['instamojo_url']);
    121     $responce_code = substr($response[0], 9, 3);
    122     $url_pieces = explode("/", $instance['instamojo_url']);
    123     if ((strpos($instance['instamojo_url'], 'www.instamojo.com') === false && $responce_code != "404") || $responce_code == "404" || count($url_pieces) != 3) {
    124       $instance['title'] = "404 error";
    125       $instance['instamojo_url'] = "#";
    126     }
    127     $instance['instamojo_url'] = 'https://'.$instance['instamojo_url'];
    12887    return $instance;
    12988  }
    13089
    13190  /**
    132    *  Implements the form() function as required by WordPress.
    133    *  This is responsible for how the form in the WordPress admin looks.
     91   *  Implements the form() function as required by WordPress
     92   *  This is responsible for how the form in the WordPress admin looks
    13493   */
    13594  function form($instance)
    13695  {
    137     $defaults = array('title' => '', 'instamojo_url' => '', 'button_pos' => 'top', 'button_style' => 'none', 'type' => true);
     96    $defaults = array('title' => '', 'instamojo_offer' => '', 'button_style' => 'none');
    13897    $instance = wp_parse_args((array)$instance, $defaults);
    139     ?>
    140     <p>
    141