Changeset 884496
- Timestamp:
- 03/30/2014 06:15:04 AM (12 years ago)
- Location:
- instamojo/trunk
- Files:
-
- 1 added
- 3 edited
-
constants.php (added)
-
instamojo-button.php (modified) (1 diff)
-
option.php (modified) (8 diffs)
-
widget.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
instamojo/trunk/instamojo-button.php
r883569 r884496 37 37 { 38 38 include_once(PLUGIN_DIR.'widget.php'); 39 register_widget(' instamojo_widget');39 register_widget('Instamojo_Widget'); 40 40 } 41 41 -
instamojo/trunk/option.php
r883568 r884496 1 1 <?php 2 2 3 define('ENCRYPTION_KEY', '!@#$%^&*'); 4 define('APPLICATION_ID', 'acd73b5ac8ccd76be2dafc46e082d415'); 5 3 include_once(__DIR__.'/constants.php'); 6 4 include_once(__DIR__.'/lib/Instamojo.php'); 7 5 … … 9 7 * Instamojo Settings Page 10 8 */ 11 class Instamojo SettingsPage9 class Instamojo_Settings_Page 12 10 { 13 11 private $_options; … … 17 15 add_action('admin_menu', array($this, 'add_plugin_page')); 18 16 add_action('admin_init', array($this, 'page_init')); 19 add_action('updated_option', array($this, 'auth_token'));20 17 } 21 18 … … 26 23 'Instamojo', 27 24 'manage_options', 28 'instamojo -admin',25 'instamojo', 29 26 array($this, 'create_admin_page') 30 27 ); … … 34 31 { 35 32 $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’ 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) 37 66 { 38 67 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>'; … … 40 69 ?> 41 70 <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> 47 105 </form> 48 106 </div> … … 54 112 register_setting( 55 113 'instamojo_credentials-group', 56 'instamojo_credentials', 57 array($this, 'sanitize') 114 'instamojo_credentials' 58 115 ); 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 needed87 *88 * @param array $input Contains all settings fields as array keys89 */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 text109 */110 public function print_section_info()111 {112 echo '<p>Enter your login credentials below:</p>';113 116 } 114 117 … … 123 126 } 124 127 125 p ublic function auth_token()128 private function revoke_token($auth_token) 126 129 { 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); 138 135 } 139 140 136 } 141 137 142 138 if (is_admin()) 143 139 { 144 $my_settings_page = new Instamojo SettingsPage();140 $my_settings_page = new Instamojo_Settings_Page(); 145 141 } 146 142 -
instamojo/trunk/widget.php
r883564 r884496 1 1 <?php 2 2 3 include_once(__DIR__.'/constants.php'); 4 include_once(__DIR__.'/lib/Instamojo.php'); 5 3 6 /** 4 * Instamojo Widget .5 * It extends the WordPress Widget class .7 * Instamojo Widget 8 * It extends the WordPress Widget class 6 9 */ 7 class instamojo_widget extends WP_Widget10 class Instamojo_Widget extends WP_Widget 8 11 { 9 12 10 11 13 /** 12 * Default constructor .14 * Default constructor 13 15 */ 14 function instamojo_widget()16 function __construct() 15 17 { 16 18 // Load any other optional scripts 17 19 add_action('load-widgets.php', array(&$this, 'my_custom_load')); 18 20 19 // Name and class of widget .21 // Name and class of widget 20 22 $widget_options = array( 21 23 'classname' => 'instamojo-widget', 22 24 'description' => 'Display Instamojo offers in your blog.'); 23 25 24 // Id, width and height of the widget .26 // Id, width and height of the widget 25 27 $control_options = array( 26 28 'id_base' => 'instamojo-widget', … … 29 31 30 32 // 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); 32 34 } 33 35 … … 41 43 42 44 /** 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 45 47 */ 46 48 function widget($args, $instance) … … 49 51 wp_enqueue_style('widgetcss'); 50 52 51 // Holds a mapping for currency to HTML of that currency. 52 $currency_html_map = array(); 53 $currency_html_map["INR"] = "₹"; 54 $currency_html_map["USD"] = "$"; 53 $instamojo_credentials = get_option('instamojo_credentials'); 55 54 56 55 extract($args); 56 if (!isset($instance['title'])) 57 { 58 $instance['title'] = ''; 59 } 57 60 $title = apply_filters('widget_title', $instance['title']); 58 61 echo $before_widget; 59 $add_feed = (substr($instance['instamojo_url'], -1) == '/') ? "feed.json" : "/feed.json";60 62 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 71 64 if ($instance['title']) { 72 65 echo $before_title.$instance['title'].$after_title; … … 76 69 } 77 70 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'].'" '; 79 72 if ($instance['button_style'] != 'none') { 80 73 $button_html .= 'class="im-checkout-btn btn--'.$instance['button_style'].'" '; 81 74 } 82 75 $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; 95 77 echo $after_widget; 96 78 } 97 79 98 80 /** 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 101 83 */ 102 84 function update($new_instance, $old_instance) 103 85 { 104 86 $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'];128 87 return $instance; 129 88 } 130 89 131 90 /** 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 134 93 */ 135 94 function form($instance) 136 95 { 137 $defaults = array('title' => '', 'instamojo_ url' => '', 'button_pos' => 'top', 'button_style' => 'none', 'type' => true);96 $defaults = array('title' => '', 'instamojo_offer' => '', 'button_style' => 'none'); 138 97 $instance = wp_parse_args((array)$instance, $defaults); 139 ?> 140 <p> 141