Example #1
0
    /**
     * Display Add User Page
     *  
     * @ Since 1.2.0
     * @ Updated 1.2.7
     */
    function admin_display_add()
    {
        $currenttab = 'add';
        $options = $this->get_all_options();
        $settings_section = $this->get_private('id') . '_' . $currenttab . '_tab';
        $submitted = isset($_POST["hidden"]) && $_POST["hidden"] == "Y" ? true : false;
        $redirect = admin_url('options-general.php?page=' . $this->get_private('settings') . '&tab=' . $currenttab);
        $success = false;
        $errormessage = null;
        $errortype = null;
        if (isset($_POST['add-user'])) {
            if (!empty($_POST['client_id']) && !empty($_POST['client_secret'])) {
                $options = $this->admin_simple_update($currenttab, $_POST, $options);
                // Don't display previously input info
                ?>
        <script>
          var url = 'https://api.instagram.com/oauth/authorize/'
          + '?redirect_uri=' + encodeURIComponent("<?php 
                echo $redirect;
                ?>
")
          + '&response_type=code'
          + '&client_id=<?php 
                echo $_POST['client_id'];
                ?>
'
          + '&display=touch';

          window.location.replace(url);
       </script>
        <?php 
            } else {
                $errormessage = 'Please enter both a Client ID and Client Secret';
            }
        } elseif (isset($_POST['delete-user']) && isset($_POST['user'])) {
            // Delete User button was pressed
            $delete = true;
            $user = $_POST['user'];
            $this->DeleteUser($user);
        } elseif (isset($_POST['update-user']) && isset($_POST['user'])) {
            // Update User button was pressed
            $user = $_POST['user'];
            $users = $this->get_instagram_users();
            if (!empty($users) && !empty($users[$user]) && !empty($users[$user]['access_token']) && !empty($users[$user]['user_id'])) {
                $request = 'https://api.instagram.com/v1/users/' . $users[$user]['user_id'] . '/?access_token=' . $users[$user]['access_token'];
                $response = wp_remote_get($request, array('method' => 'GET', 'timeout' => 10, 'sslverify' => apply_filters('https_local_ssl_verify', false)));
                if (is_wp_error($response) || !isset($response['body'])) {
                    // Try again
                    if (method_exists($this, 'manual_cURL')) {
                        $content = $this->manual_cURL($request);
                    }
                    if (!isset($content)) {
                        $errormessage = 'User not updated';
                    }
                } else {
                    $content = $response['body'];
                }
                if (isset($content)) {
                    // Before decoding JSON, remove Emoji characters from content
                    $content = $this->removeEmoji($content);
                    if (function_exists('json_decode')) {
                        $_instagram_json = @json_decode($content, true);
                    } elseif (function_exists('alpine_json_decode')) {
                        $_instagram_json = @alpine_json_decode($content, true);
                    }
                    if (empty($_instagram_json) || 200 != $_instagram_json['meta']['code']) {
                        $errormessage = 'User not updated';
                    } elseif (!empty($_instagram_json['data'])) {
                        $data = $_instagram_json['data'];
                        $post_content = array('access_token' => $users[$user]['access_token'], 'username' => $data['username'], 'picture' => $data['profile_picture'], 'fullname' => $data['full_name'], 'user_id' => $users[$user]['user_id']);
                        $this->UpdateUser($post_content);
                        $update = true;
                    }
                }
            }
        } elseif ($submitted && isset($_POST['manual-user-form']) && !empty($_POST['access_token']) && !empty($_POST['user_id']) && !empty($_POST['client_id']) && !empty($_POST['username'])) {
            $success = $this->AddUser($_POST);
        } elseif (isset($_GET['code'])) {
            // Callback has been received from Instagram
            $code = $_GET['code'];
            $client_id = $this->get_option('client_id');
            $client_secret = $this->get_option('client_secret');
            $url = 'https://api.instagram.com/oauth/access_token';
            $fields = array('code' => $code, 'response_type' => 'authorization_code', 'redirect_uri' => $redirect, 'client_id' => $client_id, 'client_secret' => $client_secret, 'grant_type' => 'authorization_code');
            $response = wp_remote_post($url, array('body' => $fields, 'sslverify' => apply_filters('https_local_ssl_verify', false)));
            $access_token = null;
            $username = null;
            $image = null;
            if (is_wp_error($response) || !isset($response['body'])) {
                // Try again
                if (method_exists($this, 'manual_cURL')) {
                    $content = $this->manual_cURL($url, $fields);
                }
                if (!isset($content)) {
                    $errormessage = 'User not added';
                }
            } else {
                $content = $response['body'];
            }
            if (isset($content)) {
                // Before decoding JSON, remove Emoji characters from content
                $content = $this->removeEmoji($content);
                if (function_exists('json_decode')) {
                    $auth = @json_decode($content, true);
                } elseif (function_exists('alpine_json_decode')) {
                    $auth = @alpine_json_decode($content, true);
                }
                // If decoded correctly, should now have access token
                if (isset($auth['access_token'])) {
                    $access_token = $auth['access_token'];
                    $user = $auth['user'];
                    $post_content = array('access_token' => $access_token, 'username' => $user['username'], 'picture' => $user['profile_picture'], 'fullname' => $user['full_name'], 'user_id' => $user['id'], 'client_id' => $client_id, 'client_secret' => $client_secret);
                    $success = $this->AddUser($post_content);
                } else {
                    $errormessage = 'No access token found';
                }
            } elseif (!is_wp_error($response) && $response['response']['code'] >= 400) {
                $error = '';
                if (function_exists('json_decode')) {
                    $error = @json_decode($response['body'], true);
                } elseif (function_exists('alpine_json_decode')) {
                    $error = @alpine_json_decode($response['body'], true);
                }
                $errormessage = $error->error_message;
                $errortype = $error->error_type;
            }
        }
        $defaults = $this->option_defaults();
        $positions = $this->get_option_positions_by_tab($currenttab);
        echo '<div class="AlpinePhotoTiles-add">';
        if (!empty($success)) {
            echo '<div class="announcement"> User successfully authorized. </div>';
        } elseif (!empty($update)) {
            echo '<div class="announcement"> User (' . $user . ') updated. </div>';
        } elseif (!empty($delete)) {
            echo '<div class="announcement"> User (' . $user . ') deleted. </div>';
        } elseif (!empty($errormessage)) {
            echo '<div class="announcement"> An error occured (' . $errormessage . '). </div>';
        }
        if (count($positions)) {
            foreach ($positions as $position => $positionsinfo) {
                if ($position == 'top') {
                    echo '<div id="AlpinePhotoTiles-user-list" style="margin-bottom:20px;padding-bottom:20px;overflow:hidden;border-bottom: 1px solid #DDDDDD;">';
                    if ($positionsinfo['title']) {
                        echo '<h4>' . $positionsinfo['title'] . '</h4>';
                    }
                    $users = $this->get_instagram_users();
                    if (empty($users) || is_array($users) && isset($users['none']) && is_array($users['none'])) {
                        echo '<p id="AlpinePhotoTiles-user-empty">No users available. Add a user by following the instructions below.</p>';
                    } elseif (!empty($users) && is_array($users)) {
                        foreach ($users as $name => $info) {
                            echo $this->show_user($info);
                            //echo '<script type = "text/javascript">'.$this->show_user_js($info).'</script>'; // Not currently needed
                        }
                    }
                    echo '</div>';
                } else {
                    echo '<div style="max-width:680px;">';
                    // Display Message about adding users
                    $this->admin_display_add_message();
                    // Show directions for Add User Method One
                    $this->admin_display_method_one($redirect);
                    echo '</div>';
                    // Display Form
                    echo '<div id="AlpinePhotoTiles-user-form" style="overflow:hidden;margin-top:20px;padding:20px;border: 1px solid #DDDDDD;">';
                    ?>
                <form id="<?php 
                    echo $this->get_private('settings') . "-add-user";
                    ?>
" action="" method="post">
                <input type="hidden" name="hidden" value="Y">
                <input type="hidden" name="add-user" value="Y">
                  <?php 
                    echo '<div class="' . $position . '">';
                    if ($positionsinfo['title']) {
                        echo '<h4>' . $positionsinfo['title'] . '</h4>';
                    }
                    echo '<table class="form-table">';
                    echo '<tbody>';
                    if (count($positionsinfo['options'])) {
                        foreach ($positionsinfo['options'] as $optionname) {
                            $option = $defaults[$optionname];
                            $fieldname = $option['name'];
                            $fieldid = $option['name'];
                            echo '<tr valign="top"><td>';
                            $this->AdminDisplayCallback(array(), $option, $fieldname, $fieldid);
                            // Don't display previously input info
                            echo '</td></tr>';
                        }
                    }
                    echo '</tbody>';
                    echo '</table>';
                    echo '</div>';
                    echo '<input id="' . $this->get_private('settings') . '-submit" name="' . $this->get_private('settings') . '_' . $currenttab . '[submit-' . $currenttab . ']" type="submit" class="button-primary" style="margin-top:15px;" value="Add and Authorize New User" />';
                    echo '</form>';
                    echo '<br style="clear:both;">';
                    echo '</div>';
                }
            }
        }
        echo '</div>';
        // close add div
        $this->admin_display_method_two();
    }
Example #2
0
 /**
  *  Function for fetching instagram feed
  *  
  *  @ Since 1.2.1
  *  @ Updated 1.2.7
  */
 function fetch_instagram_feed($request)
 {
     // No longer write out curl_init and user WP API instead
     $this->echo_point('<span style="color:blue">Use wp_remote_get() (cURL) to make request</span>');
     $response = wp_remote_get($request, array('method' => 'GET', 'timeout' => 20, 'sslverify' => false));
     $this->append_active_result('hidden', '<!-- Request made -->');
     $this->echo_point('Request complete');
     if (is_wp_error($response) || !isset($response['body'])) {
         $this->echo_point('An error occured');
         $this->append_active_result('hidden', '<!-- An error occured -->');
         if (is_wp_error($response)) {
             $this->append_active_result('hidden', '<!-- ' . $response->get_error_message() . ' -->');
             $this->echo_point('Error message: ' . $response->get_error_message());
         } elseif (!isset($response['body'])) {
             $this->echo_point('Plugin received an empty feed');
             $this->append_active_result('hidden', '<!-- No body set -->');
         }
         // Try again
         if (method_exists($this, 'manual_cURL')) {
             $this->echo_point('<span style="color:blue">Try request again using Alpine\'s cURL function</span>');
             $r = $this->manual_cURL($request, true);
             $this->echo_point('Request complete');
             if (isset($r['body']) && !empty($r['body'])) {
                 if (isset($r['code']) && !empty($r['code']) && $r['code'] >= 200 && $r['code'] < 300) {
                     // All Good
                     $content = $r['body'];
                 } else {
                     // Error
                     $this->echo_point('<span style="color:red">An error occured.</span>');
                     if (isset($r['code'])) {
                         $this->echo_point('<span style="color:red">Code</span>: ' . $r['code']);
                     }
                     $this->echo_point('Content Received: ' . esc_html($r['body']));
                     return false;
                 }
             }
         }
         if (!isset($content)) {
             $this->echo_point('No content found');
             return false;
         }
     } elseif (empty($response['body'])) {
         $this->echo_point('Plugin received an empty feed');
         $this->append_active_result('hidden', '<!-- No body content in response -->');
         return false;
     } elseif (!empty($response['response']) && !empty($response['response']['code']) && $response['response']['code'] != 200) {
         $this->echo_point('<span style="color:red">An error occured.</span>');
         $this->echo_point('<span style="color:red">Code</span>: ' . $response['response']['code']);
         if (!empty($response['response']['message'])) {
             $this->echo_point('<span style="color:red">Message</span>: ' . $response['response']['message']);
         }
         // Try again
         if (method_exists($this, 'manual_cURL')) {
             $this->echo_point('<span style="color:blue">Try request again using Alpine\'s cURL function</span>');
             $r = $this->manual_cURL($request, true);
             $this->echo_point('Request complete');
             if (isset($r['body']) && !empty($r['body'])) {
                 if (isset($r['code']) && !empty($r['code']) && $r['code'] >= 200 && $r['code'] < 300) {
                     // All Good
                     $content = $r['body'];
                 } else {
                     // Error
                     $this->echo_point('<span style="color:red">An error occured.</span>');
                     if (isset($r['code'])) {
                         $this->echo_point('<span style="color:red">Code</span>: ' . $r['code']);
                     }
                     $this->echo_point('Content Received: ' . esc_html($r['body']));
                     return false;
                 }
             }
         }
         if (!isset($content) || empty($content)) {
             $this->echo_point('No content found');
             return false;
         }
     } else {
         $this->echo_point('Content received from Instagram');
         $content = $response['body'];
     }
     if (function_exists('json_decode')) {
         $this->echo_point('<span style="color:blue">Decode content using json_decode()</span>');
         $_instagram_json = @json_decode($content, true);
         $this->echo_point('Decode completed');
     } else {
         $this->echo_point('<span style="color:red">Server is missing json_decode(). I recommend contacting your hosting provider.</span>');
         $this->echo_point('Try using alternative json decoder (Services_JSON). <span style="color:red">Loading times will increase significantly.</span>');
         $this->append_active_result('hidden', '<!-- Missing json_decode() -->');
         if (function_exists('alpine_json_decode')) {
             $_instagram_json = @alpine_json_decode($content, true);
         } else {
             $this->echo_point('<span style="color:red">Plugin is missing alpine_json_decode()</span>');
             $this->append_active_result('hidden', '<!-- Missing alpine_json_decode() -->');
         }
     }
     if (empty($_instagram_json) || !isset($_instagram_json['meta']['code'])) {
         $this->echo_point('<span style="color:red">An error occured: No JSON Data.</span>');
         $this->append_active_result('hidden', '<!-- An error occured: No JSON Data -->');
         $this->echo_point('Content Received: ' . esc_html($content));
         return false;
     } elseif (200 != $_instagram_json['meta']['code']) {
         $this->append_active_result('hidden', '<!-- An error occured: Code ' . $_instagram_json['meta']['code'] . ' -->');
         if (isset($_instagram_json['meta']['error_message'])) {
             $this->echo_point('<span style="color:red">An error occured: ' . $_instagram_json['meta']['error_type'] . ', Message: ' . $_instagram_json['meta']['error_message'] . '</span>');
             $this->append_active_result('hidden', '<!-- An error occured: Type ' . $_instagram_json['meta']['error_type'] . ', Message: ' . $_instagram_json['meta']['error_message'] . ' -->');
             $this->append_active_result('message', '<br>- ' . $_instagram_json['meta']['error_message'] . '');
         }
         return false;
     } else {
         return $_instagram_json;
     }
 }