CWS_Google_Picasa_Pro_Admin

The admin-specific functionality of the plugin.

Defined (1)

The class is defined in the following location(s).

/admin/class-cws-google-picasa-pro-admin.php  
  1. class CWS_Google_Picasa_Pro_Admin { 
  2.  
  3. var $debug = false; 
  4.  
  5. /** 
  6. * The ID of this plugin. 
  7. * @since 2.0.0 
  8. * @access private 
  9. * @var string $plugin_name The ID of this plugin. 
  10. */ 
  11. private $plugin_name; 
  12.  
  13. /** 
  14. * The version of this plugin. 
  15. * @since 2.0.0 
  16. * @access private 
  17. * @var string $version The current version of this plugin. 
  18. */ 
  19. private $version; 
  20.  
  21. /** 
  22. * The is user authenticated with Google. 
  23. * @since 2.0.0 
  24. * @access private 
  25. * @var int $authenticated Check is user authenticated with Google. 
  26. */  
  27. private $authenticated = 0; 
  28.  
  29. /** 
  30. * The check is this a Pro version. 
  31. * @since 2.0.0 
  32. * @access private 
  33. * @var int $isPro Check is if this is pro version. 
  34. */  
  35. private $isPro ;  
  36.  
  37. /** 
  38. * User Id used to check ignore upgrade notice. 
  39. * @since 2.1.1 
  40. * @access private 
  41. * @var int $userId User Id of loggedin user. 
  42. */  
  43. private $userId; 
  44.  
  45. var $client; // Used for Google Client 
  46.  
  47. /** 
  48. * Initialize the class and set its properties. 
  49. * @since 2.0.0 
  50. * @param string $plugin_name The name of this plugin. 
  51. * @param string $version The version of this plugin. 
  52. */ 
  53. public function __construct( $plugin_name, $version, $isPro ) { 
  54.  
  55. $this->plugin_name = $plugin_name; 
  56. $this->version = $version; 
  57. $this->isPro = $isPro; 
  58. $this->client = new Google_Client(); 
  59. $this->client->setApplicationName("Client_Library_Examples"); 
  60. $this->client->setDeveloperKey("AIzaSyCP9XMYoQdxXfI-gK1bvZDW2RxyfvYENuM");  
  61. $this->client->setClientId('806353319710-g782kn9ed0gm77ucl0meen5ohs84qgqm.apps.googleusercontent.com'); 
  62. $this->client->setClientSecret('P6BMMEWLKUSoxB48X2Tzu8ds'); 
  63. $this->client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob'); 
  64. $this->client->setScopes('https://picasaweb.google.com/data/'); 
  65. $this->client->setAccessType('offline'); 
  66.  
  67. // Include required files 
  68. $this->includes(); 
  69.  
  70. $this->cws_gpp_admin_installed_notice(); 
  71. //$this->cws_gpp_admin_notices_styles(); 
  72.  
  73.  
  74. /** 
  75. * Register the stylesheets for the admin area. 
  76. * @since 1.0.0 
  77. */ 
  78. public function includes() { 
  79.  
  80. if( $this->debug ) error_log( 'Inside: CWS_WPPicasaPro::includes()' ); 
  81.  
  82. if ( is_admin() ) $this->admin_includes(); 
  83.  
  84. include_once( dirname(__FILE__) . '/../cws-gpp-functions.php' ); // TODO: split file out in admin and non-admin functions 
  85. include_once( dirname(__FILE__) . '/../shortcodes/shortcode-init.php' ); // Init the shortcodes 
  86. include_once( dirname(__FILE__) . '/../widgets/widget-init.php' ); // Widget classes  
  87.  
  88. public function admin_includes() { 
  89.  
  90. if( $this->debug ) error_log( 'Inside: CWS_WPPicasaPro::admin_includes()' ); 
  91.  
  92. include_once( dirname(__FILE__) . '/../cws-gpp-functions.php' ); // TODO: split file out in admin and non-admin functions 
  93. }  
  94.  
  95. /** 
  96. * Register the stylesheets for the admin area. 
  97. * @since 2.0.0 
  98. */ 
  99. public function enqueue_styles() { 
  100.  
  101. /** 
  102. * This function is provided for demonstration purposes only. 
  103. * An instance of this class should be passed to the run() function 
  104. * defined in Plugin_Name_Loader as all of the hooks are defined 
  105. * in that particular class. 
  106. * The Plugin_Name_Loader will then create the relationship 
  107. * between the defined hooks and the functions defined in this 
  108. * class. 
  109. */ 
  110.  
  111. wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/cws-google-picasa-pro-admin.css', array(), $this->version, 'all' ); 
  112.  
  113.  
  114. /** 
  115. * Register the JavaScript for the admin area. 
  116. * @since 2.0.0 
  117. */ 
  118. public function enqueue_scripts() { 
  119.  
  120. /** 
  121. * This function is provided for demonstration purposes only. 
  122. * An instance of this class should be passed to the run() function 
  123. * defined in Plugin_Name_Loader as all of the hooks are defined 
  124. * in that particular class. 
  125. * The Plugin_Name_Loader will then create the relationship 
  126. * between the defined hooks and the functions defined in this 
  127. * class. 
  128. */ 
  129.  
  130. wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/cws-google-picasa-pro-admin.js', array( 'jquery' ), $this->version, false ); 
  131.  
  132.  
  133. /** 
  134. * Check if the plugin is a Pro version. 
  135. * @since 2.0.0 
  136. */  
  137. private function get_Pro( $isPro ) { 
  138.  
  139. if( $isPro == 1 ) { 
  140. return "Pro"; 
  141. return; 
  142.  
  143.  
  144. /** 
  145. * Register the Options for the admin area. 
  146. * @since 2.0.0 
  147. */  
  148. public function add_options_page() { 
  149.  
  150. $strIsPro = $this->get_Pro( $this->isPro ); 
  151.  
  152. // add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function); 
  153. add_options_page( 'Google Picasa ' . $strIsPro, 'Google Picasa '. $strIsPro, 'manage_options', 'cws_gpp', array( $this, 'options_page') );  
  154.  
  155.  
  156. /** 
  157. * Draw the Options page for the admin area. 
  158. * @since 2.0.0 
  159. */ 
  160. public function options_page() { 
  161.  
  162. if( $this->deauthorizeGoogleAccount() ) { 
  163. // TODO: finsish this delete_option unset() 
  164. delete_option( 'cws_gpp_reset' ); 
  165. delete_option( 'cws_gpp_token_expires' ); 
  166. delete_option( 'cws_gpp_code' ); 
  167. delete_option( 'cws_gpp_access_token' ); 
  168. } ?> 
  169.  
  170. <div class="wrap"> 
  171. <?php screen_icon(); ?> 
  172. <h2>Google Picasa <?php echo $this->get_Pro( $this->isPro );?> Settings</h2> 
  173.  
  174. <div class="widget-liquid-left"> 
  175.  
  176. <form action="options.php" method="post"> 
  177.  
  178. <?php  
  179. // Step 1: The user has not authenticated we give them a link to login  
  180. if( $this->isAuthenticated() !== true ) { 
  181. // settings_fields( $option_group ) 
  182. // Output nonce, action, and option_page fields for a settings page. Please note that this function must be called inside of the form tag for the options page. 
  183. // $option_group - A settings group name. This should match the group name used in register_setting().  
  184. settings_fields( 'cws_gpp_code' ); 
  185.  
  186. // do_settings_sections( $page ); 
  187. // Prints out all settings sections added to a particular settings page. 
  188. // The slug name of the page whose settings sections you want to output. This should match the page name used in add_settings_section(). 
  189. do_settings_sections( 'cws_gpp' );  
  190. ?> 
  191. <input name="Submit" type="submit" value="Save Changes" />  
  192.  
  193. </form>  
  194. <?php 
  195. }  
  196. else { 
  197. /** 
  198. * User is authenticated so display plugin config settings 
  199. *  
  200. */ 
  201.  
  202. // Get Access Token 
  203. $token = $this->getAccessToken(); 
  204.  
  205. settings_fields( 'cws_gpp_options' ); 
  206. do_settings_sections( 'cws_gpp_defaults' ); 
  207.  
  208. ?> 
  209. <input name="Submit" type="submit" value="Save Changes" />  
  210.  
  211. </form>  
  212.  
  213. <form action="options.php" method="post"> 
  214.  
  215. <?php 
  216. settings_fields( 'cws_gpp_reset' ); 
  217. do_settings_sections( 'cws_gpp_reset' );  
  218. ?> 
  219. <input name="Submit" type="submit" value="Deauthorise" /> 
  220.  
  221. </form>  
  222. <?php  
  223. ?> 
  224.  
  225. </div><!-- / left --> 
  226. <?php $this->cws_gpp_meta_box_feedback(); ?> 
  227.  
  228. <?php 
  229. if( !$this->isPro == 1 ) { 
  230. // Only call for the upgrade meta box if this is not a Pro install 
  231. $this->cws_gpp_meta_box_pro();  
  232. ?> 
  233.  
  234. </div> 
  235. <?php 
  236.  
  237.  
  238. // Display a feedback links 
  239. public function cws_gpp_meta_box_feedback() { 
  240. ?> 
  241.  
  242. <div class="widget-liquid-right"> 
  243. <div id="widgets-right">  
  244. <div style="width:20%;" class="postbox-container side"> 
  245. <div class="metabox-holder"> 
  246. <div class="postbox" id="feedback"> 
  247. <h3><span>Please rate the plugin!</span></h3> 
  248. <div class="inside">  
  249. <p>If you have found this useful please leave us a <a href="http://wordpress.org/extend/plugins/google-picasa-albums-viewer/">good rating</a></p> 
  250. <p>» Share it with your friends <a href="<?php echo "http://twitter.com/share?url=http://bit.ly/q4nqNA&text=Check out this awesome WordPress Plugin I'm using - Google Picasa Viewer" ?>">Tweet It</a></p> 
  251. <p>If you have found a bug please email me <a href="mailto:info@cheshirewebsolutions.com?subject=Feedback%20Google%20Picasa%20Viewer">info@cheshirewebsolutions.com</a></p>  
  252. </div> 
  253. </div> 
  254. </div> 
  255. </div>  
  256. </div> 
  257. </div>  
  258.  
  259. <?php 
  260.  
  261.  
  262.  
  263. // Display a Picasa Pro Promo Box 
  264. function cws_gpp_meta_box_pro() { 
  265. ?> 
  266. <div class="widget-liquid-right"> 
  267. <div id="widgets-right"> 
  268.  
  269. <div style="width:20%;" class="postbox-container side"> 
  270. <div class="metabox-holder"> 
  271. <div class="postbox" id="donate"> 
  272. <h3><span>Get Google Picasa Pro!</span></h3> 
  273. <div class="inside"> 
  274. <p>Grab yourself the <a href="http://www.cheshirewebsolutions.com/?utm_source=cws_gpp_config&utm_medium=text-link&utm_content=meta_box_pro&utm_campaign=cws_gpp_plugin">Pro</a> version of the plugin. <a href="http://www.cheshirewebsolutions.com/?utm_source=wp_gp_viewer&utm_medium=wp_plugin&utm_content=meta_box_download_it_here&utm_campaign=plugin_upgrade">Download it here</a></p> 
  275. <p> 
  276. Reasons to UPGRADE! 
  277. </p> 
  278. <ol> 
  279. <li>Priority Support</li> 
  280. <li>'Powerd By' link removed</li> 
  281. <li>More shortcodes for advanced layouts</li> 
  282. <li>More layouts coming soon</li> 
  283. </ol> 
  284.  
  285. </div> 
  286. </div> 
  287. </div> 
  288. </div> 
  289.  
  290.  
  291. </div> 
  292. </div> 
  293.  
  294. <?php 
  295.  
  296.  
  297.  
  298. /** 
  299. * Register Settings, Settings Section and Settings Fileds. 
  300. *  
  301. * @link https://codex.wordpress.org/Function_Reference/register_setting 
  302. * @link https://codex.wordpress.org/Function_Reference/add_settings_section 
  303. * @link https://codex.wordpress.org/Function_Reference/add_settings_field 
  304. * @since 2.0.0 
  305. */  
  306. public function register_plugin_settings() { 
  307. // register_setting( $option_group, $option_name, $sanitize_callback ). 
  308. // $option_group - A settings group name. Must exist prior to the register_setting call. This must match the group name in settings_fields(). 
  309. // $option_name - The name of an option to sanitize and save. 
  310. // $sanitize_callback - A callback function that sanitizes the option's value. 
  311. register_setting( 'cws_gpp_code', 'cws_gpp_code', array( $this, 'validate_options') ); 
  312. register_setting( 'cws_gpp_options', 'cws_gpp_options', array( $this, 'validate_main_options') ); 
  313.  
  314. register_setting( 'cws_gpp_reset', 'cws_gpp_reset', array( $this, 'validate_reset_options') ); 
  315.  
  316. // add_settings_section( $id, $title, $callback, $page ) 
  317. // $id - String for use in the 'id' attribute of tags 
  318. // $title - Title of the section 
  319. // $callback - Function that fills the section with the desired content. The function should echo its output. 
  320. // $page - The menu page on which to display this section. Should match $menu_slug in add_options_page(); 
  321. add_settings_section( 'cws_gpp_add_code', 'Authenticate with Google', array( $this, 'section_text'), 'cws_gpp' ); 
  322. add_settings_section( 'cws_gpp_add_options', 'Default Settings', array( $this, 'section_main_text'), 'cws_gpp_defaults' ); 
  323.  
  324. add_settings_section( 'cws_gpp_add_reset', 'Deauthorise Plugin from your Google Account', array( $this, 'section_reset_text'), 'cws_gpp_reset' ); 
  325.  
  326. // add_settings_field( $id, $title, $callback, $page, $section, $args ); 
  327. // $id - String for use in the 'id' attribute of tags 
  328. // $title - Title of the field 
  329. // $callback - Function that fills the field with the desired inputs as part of the larger form. Passed a single argument,  
  330. // the $args array. Name and id of the input should match the $id given to this function. The function should echo its output. 
  331. // $page - The menu page on which to display this field. Should match $menu_slug in add_options_page(); 
  332. // $section - (optional) The section of the settings page in which to show the box. A section added with add_settings_section() [optional] 
  333. // $args - (optional) Additional arguments that are passed to the $callback function 
  334. add_settings_field( 'cws_myplugin_oauth2_code', 'Enter Google Access Code here', array( $this, 'setting_input'), 'cws_gpp', 'cws_gpp_add_code' ); 
  335.  
  336.  
  337. // Add default option field - Thumbnail Size  
  338. add_settings_field( 'cws_gpp_thumbnail_size', 'Thumbnail Size (px)', array( $this, 'options_thumbnail_size'), 'cws_gpp_defaults', 'cws_gpp_add_options' ); 
  339.  
  340. // Add default option field - Album Thumbnail Size  
  341. add_settings_field( 'cws_gpp_album_thumbnail_size', 'Album Thumbnail Size (px)', array( $this, 'options_album_thumbnail_size'), 'cws_gpp_defaults', 'cws_gpp_add_options' ); 
  342.  
  343. // Add default option field - Number of image results 
  344. add_settings_field( 'cws_gpp_num_image_results', 'Number of images per page', array( $this, 'options_num_image_results'), 'cws_gpp_defaults', 'cws_gpp_add_options' ); 
  345.  
  346. // Add default option field - Number of album results 
  347. add_settings_field( 'cws_gpp_num_album_results', 'Number of albums per page', array( $this, 'options_num_album_results'), 'cws_gpp_defaults', 'cws_gpp_add_options' ); 
  348.  
  349. // Add default option field - Lightbox image size 
  350. // add_settings_field( 'cws_gpp_lightbox_image_size', 'Size of Lightbox', array( $this, 'options_lightbox_image_size'), 'cws_gpp_defaults', 'cws_gpp_add_options' ); 
  351.  
  352. // Add default option checkbox - Show private albums checkbox 
  353. add_settings_field( 'cws_gpp_show_private_albums', 'Show which albums', array( $this, 'options_show_private_albums'), 'cws_gpp_defaults', 'cws_gpp_add_options' );  
  354.  
  355. // Add default option checkbox - Show album title 
  356. add_settings_field( 'cws_gpp_show_album_title', 'Show Album Title', array( $this, 'options_show_album_title'), 'cws_gpp_defaults', 'cws_gpp_add_options' );  
  357.  
  358. // Add default option checkbox - Show image title 
  359. add_settings_field( 'cws_gpp_show_image_title', 'Show Image Title', array( $this, 'options_show_image_title'), 'cws_gpp_defaults', 'cws_gpp_add_options' );  
  360.  
  361. // Add default option checkbox - Show album details 
  362. add_settings_field( 'cws_gpp_show_album_details', 'Show Album Details', array( $this, 'options_show_album_details'), 'cws_gpp_defaults', 'cws_gpp_add_options' );  
  363.  
  364. // Add default option checkbox - Show image details 
  365. add_settings_field( 'cws_gpp_show_image_details', 'Show Image Details', array( $this, 'options_show_image_details'), 'cws_gpp_defaults', 'cws_gpp_add_options' );  
  366.  
  367. // Add reset option 
  368. add_settings_field( 'cws_gpp_reset', 'Click here to confirm you want to deauthorise plugin from your google account', array( $this, 'options_reset'), 'cws_gpp_reset', 'cws_gpp_add_reset' );  
  369.  
  370.  
  371. /** 
  372. * Draw the Section Header for the admin area. 
  373. * @since 2.0.0 
  374. */ 
  375. function section_text() { 
  376. echo 'You need to click here to authorize access and paste the Access Code provided by Google in the field below.'; 
  377.  
  378. // get the google authorisation url 
  379. $authUrl = $this->client->createAuthUrl(); 
  380.  
  381. // display the google authorisation url 
  382. echo $this->createAuthLink( $authUrl ); 
  383.  
  384. $code = get_option( 'cws_gpp_code' ); 
  385. $oauth2_code = $code['oauth2_code']; 
  386.  
  387. $token = get_option( 'cws_gpp_access_token' ); 
  388. $token = $token['access_token']; 
  389.  
  390. if ( isset( $code['oauth2_code'] ) ) { 
  391.  
  392. $this->client->authenticate( $code['oauth2_code'] );  
  393. $AccessToken = $this->client->getAccessToken(); 
  394. $AccessToken = json_decode( $AccessToken, TRUE ); 
  395.  
  396. // delete code 
  397. $code = get_option( 'cws_gpp_code' ); 
  398.  
  399. if ( $code ) { 
  400. unset($code['oauth2_code']); 
  401. update_option( 'cws_gpp_code', $code ); 
  402.  
  403. // store access token 
  404. if( update_option( 'cws_gpp_access_token', $AccessToken ) ) 
  405. if( $this->debug ) error_log( 'Update option: cws_gpp_access_token' ); 
  406.  
  407. // store token expires 
  408. $now = date("U"); 
  409. $token_expires = $now + $AccessToken['expires_in']; 
  410. add_option( 'cws_gpp_token_expires', $token_expires );  
  411.  
  412. $url = admin_url( "options-general.php?page=".$_GET["page"] ); 
  413. // error_log($url); 
  414.  
  415. wp_redirect( "$url" ); 
  416. exit;  
  417.  
  418. }  
  419.  
  420.  
  421. function section_main_text() { 
  422.  
  423.  
  424.  
  425. /** 
  426. * Get the Access Token stored in db. 
  427. * @since 2.0.0 
  428. */  
  429. public function getAccessToken() { 
  430. $token = get_option( 'cws_gpp_access_token' ); 
  431.  
  432. return $token; 
  433.  
  434.  
  435. /** 
  436. * Get the authourisation link. 
  437. * @since 2.0.0 
  438. */  
  439. public function createAuthLink( $authUrl ) { 
  440.  
  441. if ( isset( $authUrl ) ) { 
  442.  
  443. $output = "<br><br><a class='login' href='$authUrl' target='_blank'>Connect My Google Account</a>";  
  444. } else { 
  445. $output = "There was a problem generating the Google Autherisation link"; 
  446.  
  447. return $output; 
  448.  
  449. /** GOOD WORKFLOW OF STEPS https://www.domsammut.com/code/php-server-side-youtube-v3-oauth-api-video-upload-guide **/  
  450. /** 
  451. * Get the Reset option stored in db. 
  452. * @since 2.0.0 
  453. */  
  454. public function deauthorizeGoogleAccount() { 
  455. // get options from db 
  456. if( get_option( 'cws_gpp_reset' ) ) { 
  457. return true; 
  458. }  
  459.  
  460. return false; 
  461.  
  462.  
  463. public function isAuthenticated() { 
  464.  
  465. // get options from db 
  466. $code = get_option( 'cws_gpp_code' ); 
  467. $token = get_option( 'cws_gpp_access_token' ); 
  468.  
  469. if ( !isset( $token['access_token'] ) ) { 
  470. // get oauth2 code 
  471. //$this->getOAuthToken(); 
  472. else{ 
  473. // check if it needs refreshing 
  474. $now = date("U"); 
  475.  
  476. // get cws_gpp_token_expires 
  477. $token_expires = get_option( 'cws_gpp_token_expires' ); 
  478.  
  479. // check if $now is greater than cws_gpp_token_expires 
  480. if ( $now > $token_expires ) { 
  481. $this->refreshToken();  
  482. return; 
  483. }  
  484.  
  485. return true; 
  486.  
  487. return false; 
  488.  
  489.  
  490. public function refreshToken() { 
  491.  
  492. if($debug) { error_log('Inside refreshToken()'); } 
  493.  
  494. // get access token and refresh it  
  495. $now = date( "U" ); 
  496. $clientId = '806353319710-g782kn9ed0gm77ucl0meen5ohs84qgqm.apps.googleusercontent.com'; 
  497. $clientSecret = 'P6BMMEWLKUSoxB48X2Tzu8ds'; 
  498. $token = get_option( 'cws_gpp_access_token' ); 
  499. $refreshToken = $token['refresh_token']; 
  500.  
  501. $postBody = 'client_id='.urlencode($clientId) 
  502. .'&client_secret='.urlencode($clientSecret) 
  503. .'&refresh_token='.urlencode($refreshToken) 
  504. .'&grant_type=refresh_token'; 
  505.  
  506. $curl = curl_init(); 
  507. curl_setopt_array( $curl,  
  508. array( CURLOPT_CUSTOMREQUEST => 'POST' 
  509. , CURLOPT_URL => 'https://www.googleapis.com/oauth2/v3/token' 
  510. , CURLOPT_HTTPHEADER => array( 'Content-Type: application/x-www-form-urlencoded' 
  511. , 'Content-Length: '.strlen($postBody) 
  512. , CURLOPT_POSTFIELDS => $postBody  
  513. , CURLOPT_REFERER => $GOOGLE_OAUTH2_REFERER 
  514. , CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed 
  515. , CURLOPT_TIMEOUT => 12 // max seconds to wait 
  516. , CURLOPT_FOLLOWLOCATION => 0 // don't follow any Location headers, use only the CURLOPT_URL, this is for security 
  517. , CURLOPT_FAILONERROR => 0 // do not fail verbosely fi the http_code is an error, this is for security 
  518. , CURLOPT_SSL_VERIFYPEER => 0 // do verify the SSL of CURLOPT_URL, this is for security 
  519. , CURLOPT_VERBOSE => 0 // don't output verbosely to stderr, this is for security 
  520. ) ); 
  521.  
  522. $orig_response = curl_exec( $curl ); 
  523. $response = json_decode( $orig_response, true ); // convert returned objects into associative arrays 
  524. $token_expires = $now + $response['expires_in']; 
  525. $http_code = curl_getinfo( $curl, CURLINFO_HTTP_CODE ); 
  526. curl_close( $curl ); 
  527.  
  528. if ( $response['access_token'] ) { 
  529.  
  530. // get options update access token part 
  531. $token = get_option( 'cws_gpp_access_token' ); 
  532. $token['access_token'] = $response['access_token']; 
  533.  
  534. update_option("cws_gpp_access_token", $token); # save the access token 
  535. update_option("cws_gpp_token_expires", $token_expires); # save the epoch when the token expires 
  536.  
  537. $url = admin_url( "options-general.php?page=".$_GET["page"] ); 
  538. error_log($url); 
  539. error_log("********************"); 
  540. //wp_redirect( admin_url( "options-general.php?page=".$_GET["page"] ) );exit; 
  541. wp_redirect( "$url" ); 
  542. exit;  
  543.  
  544. } else { 
  545. echo "refreshOAuth2Token got the following response:<br />"; 
  546. echo $orig_response; 
  547. echo "using refreshToken $refreshToken"; 
  548. }  
  549. // TODO: Improve error handling here for various curl error codes etc  
  550.  
  551.  
  552. /** 
  553. * Get list of Albums for authenticated user. 
  554. * @since 2.0.0 
  555. */  
  556. public function getAlbumList( $AccessToken, $album_thumb_size, $show_album_ttl, $cws_page, $num_image_results, $visibility) { 
  557.  
  558. if($debug) { error_log( 'Inside getAlbumList()' ); } 
  559.  
  560. // Work out pagination variables 
  561. if ( !( isset( $cws_page ) ) ) { 
  562. $cws_page = 1; 
  563. if ( $cws_page > 1 ) { 
  564. $start_index = ( ( $cws_page - 1 ) * $num_image_results ) + 1; 
  565. } else { 
  566. $start_index = 1; 
  567. }  
  568.  
  569. $visibility = strtolower( $visibility ); 
  570.  
  571. // https://developers.google.com/accounts/docs/OAuth2WebServer#callinganapi 
  572. $curl = curl_init(); 
  573. // https://developers.google.com/picasa-web/docs/2.0/reference#Visibility 
  574. // http://picasaweb.google.com/data/feed/api/user/userID?kind=photo&q=penguin 
  575.  
  576. // limit results or not 
  577. if( $num_image_results > 0 ) { 
  578. $url = "https://picasaweb.google.com/data/feed/api/user/default?kind=album&thumbsize=" . $album_thumb_size . "c&max-results=" . $num_image_results . "&start-index=" . $start_index . "&access=" . $visibility; 
  579. } else { 
  580. $url = "https://picasaweb.google.com/data/feed/api/user/default?kind=album&thumbsize=" . $album_thumb_size . "c&access=" . $visibility; 
  581.  
  582. // TODO: create a helper function 
  583. $cws_debug = get_query_var('cws_debug'); 
  584.  
  585. if( $cws_debug == "1" ) {  
  586. echo "url = $url"; 
  587.  
  588.  
  589. curl_setopt_array( $curl,  
  590. array( CURLOPT_CUSTOMREQUEST => 'GET' 
  591. , CURLOPT_URL => $url 
  592. , CURLOPT_HTTPHEADER => array( 'GData-Version: 2' 
  593. , 'Authorization: Bearer '.$AccessToken['access_token'] ) 
  594. , CURLOPT_REFERER => 'http://wp-picasa-pro.wordpress.dev/wp-admin/options-general.php?page=cws_gpp' 
  595. , CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed 
  596. ) ); 
  597. $response = curl_exec($curl); 
  598. $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
  599. curl_close($curl); 
  600.  
  601. // Return $response to shortcode if http_code is 200 
  602. if( $http_code == 200 ) { 
  603. return $response; 
  604. // TODO: Add in some error handling better reporting of http code 
  605. return false; 
  606.  
  607.  
  608. /** 
  609. * Get list of Images in a specific Album for authenticated user. 
  610. * @since 2.0.0 
  611. */ 
  612. public function getAlbumImages( $AccessToken, $album_thumb_size, $show_album_ttl, $cws_page, $num_image_results, $cws_album ) { 
  613.  
  614. if($debug) { error_log('Inside getAlbumImages()'); } 
  615.  
  616. // Work out pagination variables 
  617. if ( !( isset( $cws_page ) ) ) { 
  618. $cws_page = 1; 
  619. if ( $cws_page > 1 ) { 
  620. $start_index = ( ( $cws_page - 1 ) * $num_image_results ) + 1; 
  621. } else { 
  622. $start_index = 1; 
  623. }  
  624.  
  625. $curl = curl_init(); 
  626.  
  627.  
  628. if( $num_image_results > 0 ) { 
  629. $url = "https://picasaweb.google.com/data/feed/api/user/default/albumid/" . $cws_album . "?thumbsize=" . $album_thumb_size . "c&max-results=" . $num_image_results . "&start-index=" . $start_index; 
  630. } else { 
  631. $url = "https://picasaweb.google.com/data/feed/api/user/default/albumid/" . $cws_album . "?thumbsize=" . $album_thumb_size . "c"; 
  632. // TODO: create a helper function 
  633. $cws_debug = get_query_var('cws_debug'); 
  634.  
  635. curl_setopt_array( $curl,  
  636. array( CURLOPT_CUSTOMREQUEST => 'GET' 
  637. , CURLOPT_URL => $url 
  638. , CURLOPT_HTTPHEADER => array( 'GData-Version: 2' 
  639. , 'Authorization: Bearer '.$AccessToken['access_token'] ) 
  640. , CURLOPT_REFERER => 'http://wp-picasa-pro.wordpress.dev/wp-admin/options-general.php?page=cws_gpp' 
  641. , CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed 
  642. ) ); 
  643. $response = curl_exec($curl); 
  644. $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
  645. curl_close($curl); 
  646.  
  647.  
  648. if($cws_debug == "1") {  
  649. echo "url = $url"; 
  650.  
  651. echo "http code: $http_code<br>"; 
  652. echo '<pre>'; 
  653. print_r($response); 
  654. echo '</pre>';  
  655.  
  656.  
  657. // Return $response to shortcode if http_code is 200 
  658. if( $http_code == 200 ) { 
  659. return $response; 
  660.  
  661.  
  662. /** 
  663. * Pagination Helper 
  664. * $num_pages, int 
  665. * $current_page, int 
  666. * $album_id 
  667. * return string 
  668. */  
  669. public function get_pagination( $total_num_albums, $num_image_results, $cws_page, $album_id ) { 
  670.  
  671. if( $this->debug ) error_log( 'Inside: CWS_Google_Picasa_Pro_Admin::get_pagination()' );  
  672.  
  673. // Calcualte how many pages we need, total number of albums / number of images to display per page as set in settings of shortcode 
  674. $num_pages = ceil( $total_num_albums / $num_image_results ); 
  675.  
  676. // If ony need one page then do not display pagination 
  677. if ( $num_pages <= 1) { 
  678. return; 
  679.  
  680. if( ! isset( $cws_page ) || $cws_page < 1 ) { $cws_page = 1; } // TODO: Do we need this check? 
  681.  
  682. // Create page links 
  683. $html[] = "<div class=\"pagination\"><ul class=\"page-nav\">\n"; 
  684.  
  685. $previous = $cws_page - 1; 
  686. $next = $cws_page + 1; 
  687.  
  688. // Previous link 
  689. if( $previous > 0 ) { 
  690.  
  691. // if have album id, i.e. display this on the results page 
  692. if( $album_id ) { 
  693. $html[] = "<li><a href=\"?cws_page=$previous&cws_album=$album_id\" id='prev_page'>Previous</a></li>"; 
  694. } else { 
  695. $html[] = "<li><a href=\"?cws_page=$previous\" id='prev_page'>Previous</a></li>";  
  696.  
  697. for( $i=1 ; $i <= $num_pages ; $i++ ) { 
  698.  
  699. $class = ""; 
  700.  
  701. // Add class to current page 
  702. if( $i == $cws_page) { 
  703. $class = " class='selected'"; 
  704.  
  705. $html[] = "<li".$class.">"; 
  706.  
  707.  
  708. if( $album_id ) { 
  709. $html[] = "<a href=\"?cws_page=$i&cws_album=$album_id\" id='page_".$i."'>".$i."</a></li>\n"; 
  710. } else { 
  711. $html[] = "<a href=\"?cws_page=$i\" id='page_".$i."'>".$i."</a></li>\n"; 
  712.  
  713. // Next link 
  714. if( $next <= $num_pages ) { 
  715.  
  716. // if have album id 
  717. if( $album_id ) { 
  718. $html[] = "<li><a href=\"?cws_page=$next&cws_album=$album_id\" id='next_page'>Next</a></li>"; 
  719. } else { 
  720. $html[] = "<li><a href=\"?cws_page=$next\" id='next_page'>Next</a></li>"; 
  721.  
  722. if( !$this->isPro == 1 ) { 
  723. $html[] = "</ul><span>Powered by: <a href=\"http://www.cheshirewebsolutions.com/\">Cheshire Web Solutions</a></span></div>\n"; 
  724. } else { 
  725. $html[] = "</ul></div>\n"; 
  726.  
  727. return implode( "\n", $html ); 
  728.  
  729.  
  730. /** 
  731. * Display and fill the form field. 
  732. * @since 2.0.0 
  733. */  
  734. function setting_input() { 
  735.  
  736. // get option 'oauth2_code' value from the database 
  737. $code = get_option( 'cws_gpp_code' ); 
  738. $oauth2_code = $code['oauth2_code']; 
  739. // echo the field 
  740. echo "<input id='oauth2_code' name='cws_gpp_code[oauth2_code]' type='text' value='$oauth2_code' >"; 
  741.  
  742.  
  743. /** 
  744. * Display and fill the form fields for storing defaults. 
  745. * Thumbnail Size in pixels 
  746. * @since 2.0.0 
  747. */  
  748. function options_thumbnail_size() { 
  749.  
  750. // get option 'thumb_size' value from the database 
  751. $options = get_option( 'cws_gpp_options' ); 
  752. $thumb_size = $options['thumb_size']; 
  753.  
  754. echo "<input id='thumb_size' name='cws_gpp_options[thumb_size]' type='text' value='$thumb_size' >"; 
  755. }  
  756.  
  757.  
  758. /** 
  759. * Display and fill the form fields for storing defaults. 
  760. * Album Thumbnail Size in pixels 
  761. * @since 2.0.0 
  762. */  
  763. function options_album_thumbnail_size() { 
  764.  
  765. // get option 'album_thumb_size' value from the database 
  766. $options = get_option( 'cws_gpp_options' ); 
  767. $album_thumb_size = $options['album_thumb_size']; 
  768.  
  769. echo "<input id='album_thumb_size' name='cws_gpp_options[album_thumb_size]' type='text' value='$album_thumb_size' >"; 
  770. }  
  771.  
  772.  
  773. /** 
  774. * Display and fill the form fields for storing defaults. 
  775. * Number of images results to display per page 
  776. * @since 2.0.0 
  777. */  
  778. function options_num_image_results() { 
  779.  
  780. // get option 'num_image_results' value from the database 
  781. $options = get_option( 'cws_gpp_options' ); 
  782. $num_image_results = $options['num_image_results']; 
  783.  
  784. echo "<input id='num_image_results' name='cws_gpp_options[num_image_results]' type='text' value='$num_image_results' >"; 
  785. }  
  786.  
  787.  
  788. /** 
  789. * Display and fill the form fields for storing defaults. 
  790. * Number of albums results to display per page 
  791. * @since 2.0.0 
  792. */  
  793. function options_num_album_results() { 
  794.  
  795. // get option 'num_album_results' value from the database 
  796. $options = get_option( 'cws_gpp_options' ); 
  797. $num_album_results = $options['num_album_results']; 
  798.  
  799. echo "<input id='num_album_results' name='cws_gpp_options[num_album_results]' type='text' value='$num_album_results' >"; 
  800. }  
  801.  
  802.  
  803. /** 
  804. * Display and fill the form fields for storing defaults. 
  805. * Lightbox image size 
  806. * @since 2.0.0 
  807. */  
  808. function options_lightbox_image_size() { 
  809.  
  810. // get option 'lightbox_image_size' value from the database 
  811. $options = get_option( 'cws_gpp_options' ); 
  812. $lightbox_image_size = $options['lightbox_image_size']; 
  813.  
  814. echo "<input id='lightbox_image_size' name='cws_gpp_options[lightbox_image_size]' type='text' value='$lightbox_image_size' >"; 
  815.  
  816.  
  817. /** 
  818. * Display and fill the form fields for storing defaults. 
  819. * Show Private Albums 
  820. * @since 2.0.0 
  821. */  
  822. function options_show_private_albums() { 
  823.  
  824. // DROP-DOWN-BOX - Name: plugin_options[dropdown1] 
  825. $options = get_option('cws_gpp_options'); 
  826. $items = array("All", "Private", "Public"); 
  827. echo "<select id='cws_gpp_show_private_albums' name='cws_gpp_options[private_albums]'>"; 
  828. foreach($items as $item) { 
  829. $selected = ($options['private_albums']==$item) ? 'selected="selected"' : ''; 
  830. echo "<option value='$item' $selected>$item</option>"; 
  831. echo "</select>"; 
  832.  
  833.  
  834. /** 
  835. * Display and fill the form fields for storing defaults. 
  836. * Show Album Title 
  837. * @since 2.0.0 
  838. */  
  839. function options_show_album_title() { 
  840.  
  841. // get option 'show_album_title' value from the database 
  842. $options = get_option( 'cws_gpp_options' ); 
  843. $show_album_title = $options['show_album_title']; 
  844.  
  845. if($options['show_album_title']) { $checked = ' checked="checked" '; } 
  846. echo "<input ".$checked." id='show_album_title' name='cws_gpp_options[show_album_title]' type='checkbox' />"; 
  847. }  
  848.  
  849.  
  850. /** 
  851. * Display and fill the form fields for storing defaults. 
  852. * Show Image Title 
  853. * @since 2.0.0 
  854. */  
  855. function options_show_image_title() { 
  856.  
  857. // get option 'show_image_title' value from the database 
  858. $options = get_option( 'cws_gpp_options' ); 
  859. $show_album_title = $options['show_image_title']; 
  860.  
  861. if($options['show_image_title']) { $checked = ' checked="checked" '; } 
  862. echo "<input ".$checked." id='show_image_title' name='cws_gpp_options[show_image_title]' type='checkbox' />"; 
  863. }  
  864.  
  865.  
  866. /** 
  867. * Display and fill the form fields for storing defaults. 
  868. * Show Album Details 
  869. * @since 2.0.0 
  870. */  
  871.  
  872. function options_show_album_details() { 
  873.  
  874. // get option 'show_album_details' value from the database 
  875. $options = get_option( 'cws_gpp_options' ); 
  876. $show_album_details = $options['show_album_details']; 
  877.  
  878. if($options['show_album_details']) { $checked = ' checked="checked" '; } 
  879. echo "<input ".$checked." id='show_album_details' name='cws_gpp_options[show_album_details]' type='checkbox' />"; 
  880. }  
  881.  
  882.  
  883. /** 
  884. * Display and fill the form fields for storing defaults. 
  885. * Show Image Details 
  886. * @since 2.0.0 
  887. */  
  888. function options_show_image_details() { 
  889.  
  890. // get option 'show_album_details' value from the database 
  891. $options = get_option( 'cws_gpp_options' ); 
  892. $show_image_details = $options['show_image_details']; 
  893.  
  894. if($options['show_image_details']) { $checked = ' checked="checked" '; } 
  895. echo "<input ".$checked." id='show_image_details' name='cws_gpp_options[show_image_details]' type='checkbox' />"; 
  896. }  
  897.  
  898.  
  899. /** 
  900. * Display and fill the form fields for storing defaults. 
  901. * Show Album Details 
  902. * @since 2.0.0 
  903. */  
  904. function options_reset() { 
  905.  
  906. // get option 'show_album_details' value from the database 
  907. // $options = get_option( 'cws_gpp_options' ); 
  908. //$show_album_details = $options['show_album_details']; 
  909.  
  910. if($options['cws_gpp_reset']) { $checked = ' checked="checked" '; } 
  911. echo "<input ".$checked." id='cws_gpp_reset' name='cws_gpp_reset[reset]' type='checkbox' required />"; 
  912. }  
  913.  
  914.  
  915. /** 
  916. * Validate user input (we want text only). 
  917. * @since 2.0.0 
  918. */  
  919. function validate_options( $input ) { 
  920.  
  921. $valid['oauth2_code'] = esc_attr ( $input['oauth2_code'] ); 
  922.  
  923. return $valid; 
  924.  
  925.  
  926. /** 
  927. * Validate user input. 
  928. * @since 2.0.0 
  929. */  
  930. function validate_main_options( $input ) { 
  931.  
  932. $valid['thumb_size'] = esc_attr( $input['thumb_size'] ); 
  933. $valid['album_thumb_size'] = esc_attr( $input['album_thumb_size'] ); 
  934. $valid['num_image_results'] = esc_attr( $input['num_image_results'] ); 
  935. $valid['num_album_results'] = esc_attr( $input['num_album_results'] ); 
  936. $valid['lightbox_image_size'] = esc_attr( $input['lightbox_image_size'] ); 
  937. $valid['private_albums'] = esc_attr( $input['private_albums'] ); 
  938.  
  939. // Correct validation of checkboxes 
  940. $valid[show_album_title] = ( isset( $input[show_album_title] ) && true == $input[show_album_title] ? true : false ); 
  941. $valid[show_album_details] = ( isset( $input[show_album_details] ) && true == $input[show_album_details] ? true : false ); 
  942.  
  943. $valid[show_image_title] = ( isset( $input[show_image_title] ) && true == $input[show_image_title] ? true : false ); 
  944. // $valid[show_image_details] = ( isset( $input[show_image_details] ) && true == $input[show_image_details] ? true : false ); 
  945.  
  946. return $valid; 
  947. }  
  948.  
  949.  
  950. /** 
  951. * Validate user input. 
  952. * @since 2.0.0 
  953. */  
  954. function validate_reset_options( $input ) { 
  955.  
  956. // Correct validation of checkboxes 
  957. $valid[reset] = ( isset( $input[reset] ) && true == $input[reset] ? true : false ); 
  958.  
  959. return $valid; 
  960. }  
  961.  
  962.  
  963. // Dispay upgrade notice 
  964. function cws_gpp_admin_installed_notice() { 
  965.  
  966. // http://wordpress.stackexchange.com/questions/134826/trying-to-get-logged-in-user-data-inside-php-class 
  967.  
  968.  
  969. add_action( 'wp_loaded', function() { 
  970. $current_user = getCurrentUser(); 
  971. if ( $current_user instanceof WP_PM_User ) { 
  972. //echo 'Current user ID: ' . $current_user->getID() . '(class)'; 
  973.  
  974. // check if already Pro 
  975. if(!$this->isPro) { 
  976.  
  977. //var_dump($this->isPro); 
  978.  
  979. // Check if user has dismissed notice previously 
  980. if ( ! get_user_meta( $current_user->getID(), 'cws_gpp_ignore_upgrade' ) )  
  981. global $pagenow; 
  982. // Only show upgrade notice if on this page 
  983. //if ( $pagenow == 'plugins.php' )  
  984. if ( $pagenow == 'options-general.php' ) 
  985. ?> 
  986. <div id="message" class="updated cws-gpp-message"> 
  987. <div class="squeezer"> 
  988. <h4><?php _e( '<strong>Google Photos / Picasa Viewer has been installed – Get the Pro version</strong>', 'woocommerce' ); ?></h4> 
  989. <p class="submit"> 
  990. <a href="http://www.cheshirewebsolutions.com/?utm_source=cws_gpp_config&utm_medium=button&utm_content=upgrade_notice_message&utm_campaign=cws_gpp_plugin" class="button-primary"><?php _e( 'Visit Site', 'cws_gpp_' ); ?></a> 
  991. <a href="<?php echo admin_url('admin.php?page=cws_gpp'); ?>" class="button-primary"><?php _e( 'Settings', 'woocommerce' ); ?></a> 
  992. <a href="?cws_gpp_ignore_upgrade=0" class="secondary-button">Hide Notice</a> 
  993. </p> 
  994. </div> 
  995. </div> 
  996. <?php 
  997.  
  998.  
  999.  
  1000. // Set installed option 
  1001. update_option('cws_gpp_installed', 0); 
  1002.  
  1003. // 
  1004.   
  1005.  
  1006. } else { 
  1007. //echo 'No one logged in (class)'; 
  1008.  
  1009. }, 30 ); 
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023. // If installed display upgrade notice 
  1024. function cws_gpp_admin_notices_styles() { 
  1025.  
  1026. // // Installed notices 
  1027. if ( get_option( 'cws_gpp_installed' ) == 1 ) { 
  1028. add_action( 'admin_notices', 'cws_gpp_admin_installed_notice' );  
  1029.  
  1030. // Allow user to dismiss upgrade notice :) 
  1031. // moved line below to define_admin_hooks(); 
  1032. //add_action( 'admin_init', 'cws_gpp_ignore_upgrade' );  
  1033. function cws_gpp_ignore_upgrade() {  
  1034. $user_id = $current_user->ID; 
  1035.  
  1036. /** If user clicks to ignore the notice, add that to their user meta */ 
  1037. if ( isset( $_GET['cws_gpp_ignore_upgrade'] ) && '0' == $_GET['cws_gpp_ignore_upgrade'] ) { 
  1038. add_user_meta($this->userId, 'cws_gpp_ignore_upgrade', 'true', true); 
  1039.  
  1040. // can I redirect user to plugin settings page ? 
  1041. wp_redirect( admin_url('options-general.php?page=cws_gpp', 'http') ); 
  1042. }  
  1043.  
  1044. // 
  1045.   
  1046.