diff --git a/openphoto-wordpress.php b/openphoto-wordpress.php index 8a61fbd..516da8c 100644 --- a/openphoto-wordpress.php +++ b/openphoto-wordpress.php @@ -9,6 +9,7 @@ Description: Connects a WordPress installation to an OpenPhoto installation. */ new WP_OpenPhoto; +include ('openphoto-php/OpenPhotoOAuth.php'); class WP_OpenPhoto { @@ -19,7 +20,7 @@ class WP_OpenPhoto { function __construct() { - new WP_OpenPhoto_Options; + new WP_OpenPhoto_Settings; add_filter('media_upload_tabs', array( &$this, 'media_add_openphoto_tab' )); add_action('media_upload_openphoto', array( &$this, 'media_include_openphoto_iframe')); } @@ -35,27 +36,203 @@ class WP_OpenPhoto { function media_render_openphoto_tab() { media_upload_header(); - echo '
'; - } + $openphoto = get_option('openphoto_wordpress_settings'); + + $curl_get = '?'; + $curl_get .= 'oauth_consumer_key=' . $openphoto["oauth_consumer_key"]; + $curl_get .= '&oauth_consumer_secret=' . $openphoto["oauth_consumer_secret"]; + $curl_get .= '&oauth_token=' . $openphoto["oauth_token"]; + $curl_get .= '&oauth_token_secret=' . $openphoto["oauth_token_secret"]; + $curl_get .= '&returnSizes=32x32,128x128'; + $curl_options = array( + CURLOPT_HEADER => 0, + CURLOPT_URL => trailingslashit($openphoto['host']) . 'photos/list.json' . $curl_get, + CURLOPT_FRESH_CONNECT => 1, + CURLOPT_RETURNTRANSFER => 1, + ); + $ch = curl_init(); + curl_setopt_array($ch, $curl_options); + $response = curl_exec($ch); + curl_close($ch); + + $response = json_decode($response); + $photos = $response->result; + + if ($photos) + { + + echo ''; + + } + } } -class WP_OpenPhoto_Options { +class WP_OpenPhoto_Settings { - function WP_OpenPhoto_Options() + function WP_OpenPhoto_Settings() { - $this->__construct(); + $this->__construct(); } function __construct() { - # Place your add_actions and add_filters here - } // function + add_action('admin_init', array( &$this, 'settings_init')); + add_action('admin_menu', array( &$this, 'settings_add_openphoto_page')); + } - function admin_init() + function settings_init() { + register_setting( 'openphoto_wordpress_settings', 'openphoto_wordpress_settings', array(&$this,'settings_validate_submission')); + } + + function settings_add_openphoto_page() { - //add_action( 'admin_init', array( &$this, 'admin_init' ) ); - //add_action( 'init', array( &$this, 'init' ) ); - } // function + add_options_page('Configure OpenPhoto Integration', 'OpenPhoto', 'manage_options', 'openphoto_wordpress_settings', array( &$this, 'settings_render_openphoto_page')); + } + + function settings_render_openphoto_page() + { + + $options = get_option('openphoto_wordpress_settings'); + $auto_submit_js = false; + + // if all the values are set + if (isset($_REQUEST["oauth_consumer_key"]) && $_REQUEST["oauth_consumer_key"] != "" && + isset($_REQUEST["oauth_consumer_secret"]) && + isset($_REQUEST["oauth_token"]) && + isset($_REQUEST["oauth_token_secret"]) && + isset($_REQUEST["oauth_verifier"]) ) + { + + // if even one of the values in the database is different than those in the request + if ($options['oauth_consumer_key'] != $_REQUEST["oauth_consumer_key"] || + $options['oauth_consumer_secret'] != $_REQUEST["oauth_consumer_secret"] || + $options['unauthorized_token'] != $_REQUEST["oauth_token"] || + $options['unauthorized_token_secret'] != $_REQUEST["oauth_token_secret"] || + $options['oauth_verifier'] != $_REQUEST["oauth_verifier"]) + { + + // load the values from the request into the input boxes + $options['oauth_consumer_key'] = $_REQUEST["oauth_consumer_key"]; + $options['oauth_consumer_secret'] = $_REQUEST["oauth_consumer_secret"]; + $options['unauthorized_token'] = $_REQUEST["oauth_token"]; + $options['unauthorized_token_secret'] = $_REQUEST["oauth_token_secret"]; + $options['oauth_verifier'] = $_REQUEST["oauth_verifier"]; + + $curl_post = array('oauth_consumer_key' => $_REQUEST["oauth_consumer_key"],'oauth_consumer_secret' => $_REQUEST["oauth_consumer_secret"], 'oauth_token' => $_REQUEST["oauth_token"], 'oauth_token_secret' => $_REQUEST["oauthoauth_token_secret_token"], 'oauth_token_secret' => $_REQUEST["oauthoauth_token_secret_token"], 'oauth_verifier' => $_REQUEST['oauth_verifier']); + $curl_options = array( + CURLOPT_POST => 1, + CURLOPT_HEADER => 0, + CURLOPT_URL => trailingslashit($options['host']) . 'v1/oauth/token/access', + CURLOPT_FRESH_CONNECT => 1, + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_POSTFIELDS => http_build_query($curl_post) + ); + $ch = curl_init(); + curl_setopt_array($ch, $curl_options); + $response = curl_exec($ch); + curl_close($ch); + + $authorized = wp_parse_args($response); + $options['oauth_token'] = $authorized['oauth_token']; + $options['oauth_token_secret'] = $authorized['oauth_token_secret']; + + // hide the values and submit the form on page load + $auto_submit_js = true; + + } + } + + if ($options["host_changed"] && $_REQUEST["settings-updated"]=="true" && $auto_submit_js==false) + { + $auto_redirect_js = true; + } + + if ($auto_submit_js==true || $auto_redirect_js==true) echo ''; + + echo '