private function s3_delete($S3_bucket, $S3_key)
 {
     $delete_result = false;
     if ($s3 = $this->s3($S3_bucket)) {
         $delete_result = $s3->object_exists($S3_key) ? $s3->delete($S3_key) : true;
         if (self::$debug_mode && function_exists('dbgx_trace_var')) {
             dbgx_trace_var($delete_result);
         }
     }
     return $delete_result;
 }
 public function options_save()
 {
     $option_keys = $this->option_keys();
     $this->options = $this->get_option();
     $iv = new InputValidator('POST');
     $iv->set_rules(self::NONCE_NAME, 'required');
     // Update options
     if (!is_wp_error($iv->input(self::NONCE_NAME)) && check_admin_referer(self::NONCE_ACTION, self::NONCE_NAME)) {
         // Get posted options
         $fields = array_keys($option_keys);
         foreach ($fields as $field) {
             switch ($field) {
                 case 'access_key':
                 case 'secret_key':
                     $iv->set_rules($field, array('trim', 'esc_html', 'required'));
                     break;
                 default:
                     $iv->set_rules($field, array('trim', 'esc_html'));
                     break;
             }
         }
         $options = $iv->input($fields);
         $err_message = '';
         foreach ($option_keys as $key => $field) {
             if (is_wp_error($options[$key])) {
                 $error_data = $options[$key];
                 $err = '';
                 foreach ($error_data->errors as $errors) {
                     foreach ($errors as $error) {
                         $err .= (!empty($err) ? '<br />' : '') . __('Error! : ', self::TEXT_DOMAIN);
                         $err .= sprintf(__(str_replace($key, '%s', $error), self::TEXT_DOMAIN), $field);
                     }
                 }
                 $err_message .= (!empty($err_message) ? '<br />' : '') . $err;
             }
             if (!isset($options[$key]) || is_wp_error($options[$key])) {
                 $options[$key] = '';
             }
         }
         if (self::$debug_mode && function_exists('dbgx_trace_var')) {
             dbgx_trace_var($options);
         }
         // Update options
         if ($this->options !== $options) {
             update_option(self::OPTION_KEY, $options);
             printf('<div id="message" class="updated fade"><p><strong>%s</strong></p></div>' . "\n", empty($err_message) ? __('Done!', self::TEXT_DOMAIN) : $err_message);
             $this->options = $options;
         }
         unset($options);
     }
 }
    public function options_page()
    {
        $nonce_action = 'update_options';
        $nonce_name = '_wpnonce_update_options';
        $option_keys = $this->option_keys();
        $option_keys = apply_filters('nephila_clavata_option_keys', $option_keys);
        self::$options = $this->get_option();
        $title = __('Nephila clavata', NephilaClavata::TEXT_DOMAIN);
        $iv = new InputValidator('POST');
        $iv->set_rules($nonce_name, 'required');
        // Update options
        if (!is_wp_error($iv->input($nonce_name)) && check_admin_referer($nonce_action, $nonce_name)) {
            // Get posted options
            $fields = array_keys($option_keys);
            foreach ($fields as $field) {
                switch ($field) {
                    case 'access_key':
                    case 'secret_key':
                        $iv->set_rules($field, array('trim', 'esc_html', 'required'));
                        break;
                    default:
                        $iv->set_rules($field, array('trim', 'esc_html'));
                        break;
                }
            }
            $options = $iv->input($fields);
            $err_message = '';
            foreach ($option_keys as $key => $field) {
                if (is_wp_error($options[$key])) {
                    $error_data = $options[$key];
                    $err = '';
                    foreach ($error_data->errors as $errors) {
                        foreach ($errors as $error) {
                            $err .= (!empty($err) ? '<br />' : '') . __('Error! : ', NephilaClavata::TEXT_DOMAIN);
                            $err .= sprintf(__(str_replace($key, '%s', $error), NephilaClavata::TEXT_DOMAIN), $field);
                        }
                    }
                    $err_message .= (!empty($err_message) ? '<br />' : '') . $err;
                }
                if (!isset($options[$key]) || is_wp_error($options[$key])) {
                    $options[$key] = '';
                }
            }
            if (empty($options['s3_url']) && !empty($options['bucket'])) {
                $options['s3_url'] = sprintf('http://%1$s.s3-website-%2$s.amazonaws.com', strtolower($options['bucket']), strtolower(str_replace('_', '-', $options['region'])));
            }
            if (!empty($options['s3_url']) && !preg_match('#^https?://#i', $options['s3_url'])) {
                $options['s3_url'] = 'http://' . preg_replace('#^//?#', '', $options['s3_url']);
            }
            $options['s3_url'] = untrailingslashit($options['s3_url']);
            if (NephilaClavata::DEBUG_MODE && function_exists('dbgx_trace_var')) {
                dbgx_trace_var($options);
            }
            // Update options
            if (self::$options !== $options) {
                update_option(self::OPTION_KEY, $options);
                if (self::$options['s3_url'] !== $options['s3_url']) {
                    global $wpdb;
                    $sql = $wpdb->prepare("delete from {$wpdb->postmeta} where meta_key in (%s, %s)", NephilaClavata::META_KEY, NephilaClavata::META_KEY . '-replace');
                    $wpdb->query($sql);
                }
                printf('<div id="message" class="updated fade"><p><strong>%s</strong></p></div>' . "\n", empty($err_message) ? __('Done!', NephilaClavata::TEXT_DOMAIN) : $err_message);
                self::$options = $options;
            }
            unset($options);
        }
        // Get S3 Object
        $s3 = S3_helper::get_instance();
        $s3->init(isset(self::$options['access_key']) ? self::$options['access_key'] : null, isset(self::$options['secret_key']) ? self::$options['secret_key'] : null, isset(self::$options['region']) ? self::$options['region'] : null);
        $regions = $this->regions;
        $buckets = false;
        if ($s3) {
            $regions = $s3->get_regions();
            $buckets = $s3->list_buckets();
        }
        if (!$buckets) {
            unset($option_keys['bucket']);
            unset($option_keys['s3_url']);
        }
        $storage_classes = $this->storage_classes;
        ?>
		<div class="wrap">
		<?php 
        screen_icon();
        ?>
		<h2><?php 
        echo esc_html($title);
        ?>
</h2>
		<form method="post" action="<?php 
        echo $this->admin_action;
        ?>
">
		<?php 
        echo wp_nonce_field($nonce_action, $nonce_name, true, false) . "\n";
        ?>
		<table class="wp-list-table fixed"><tbody>
		<?php 
        foreach ($option_keys as $field => $label) {
            $this->input_field($field, $label, array('regions' => $regions, 'buckets' => $buckets, 'storage_classes' => $storage_classes));
        }
        ?>
		</tbody></table>
		<?php 
        submit_button();
        ?>
		</form>
		</div>
<?php 
    }
 private function replace_s3_url($content, $post_id)
 {
     global $pagenow;
     if (empty($content)) {
         return $content;
     }
     $s3_bucket = isset(self::$options['bucket']) ? self::$options['bucket'] : false;
     $s3_url = isset(self::$options['s3_url']) ? self::$options['s3_url'] : false;
     $storage_class = isset(self::$options['storage_class']) ? self::$options['storage_class'] : false;
     if (!$s3_bucket || !$s3_url) {
         return $content;
     }
     // get attachments
     $attachment_meta_key = self::META_KEY;
     $post_meta_key = self::META_KEY . '-replace';
     if ($post_id) {
         if ($replace_data = get_post_meta($post_id, $post_meta_key, true)) {
             $replace_data_org = isset($replace_data[$s3_bucket]) ? $replace_data[$s3_bucket] : array();
             foreach ($replace_data_org as $search_url => $replace_url) {
                 $content = str_replace($search_url, $replace_url, $content);
             }
         }
         $attachments = $this->get_post_attachments($post_id, $content);
         if (self::DEBUG_MODE && function_exists('dbgx_trace_var')) {
             dbgx_trace_var($post_id);
             dbgx_trace_var($attachments);
         }
         if (!$attachments) {
             return $content;
         }
     } else {
         $attachments_count = 0;
         $medias = array();
         $attachments = $this->get_post_attachments_from_content($content, $attachments_count, $medias);
         if (self::DEBUG_MODE && function_exists('dbgx_trace_var')) {
             dbgx_trace_var($attachments);
         }
         if (!$attachments) {
             return $content;
         }
     }
     // object upload to S3, and replace url
     $replace_count = 0;
     $replace_data = array();
     $attachments_meta = array();
     foreach ($attachments as $attachment) {
         $S3_medias_new = $this->get_s3_media_info($attachment->ID);
         $S3_medias_old = get_post_meta($attachment->ID, $attachment_meta_key, true);
         if (is_array($S3_medias_old)) {
             if (isset($S3_medias_old[$s3_bucket])) {
                 $S3_medias_old = (array) $S3_medias_old[$s3_bucket];
             }
         } else {
             $S3_medias_old = array();
         }
         if (self::DEBUG_MODE && function_exists('dbgx_trace_var')) {
             dbgx_trace_var($S3_medias_new);
             dbgx_trace_var($S3_medias_old);
         }
         if ($S3_medias_new !== $S3_medias_old) {
             $S3_medias = array();
             foreach ($S3_medias_new as $size => $val) {
                 $replace_flag = true;
                 $search_url = $val['url'];
                 $replace_url = $s3_url . $val['s3_key'];
                 if (!isset($S3_medias_old[$size])) {
                     $result = $this->s3_upload($val['file'], $s3_bucket, $val['s3_key'], $storage_class);
                     $replace_flag = !$result ? false : true;
                     if (self::DEBUG_MODE && function_exists('dbgx_trace_var')) {
                         dbgx_trace_var($result);
                         dbgx_trace_var($val);
                     }
                 }
                 if ($replace_flag && $search_url && $replace_url) {
                     $S3_medias[$size] = $val;
                     if (!isset($replace_data[$search_url]) && (!is_admin() || $pagenow === 'upload.php')) {
                         $replace_data[$search_url] = $replace_url;
                         $content = str_replace($search_url, $replace_url, $content);
                     }
                 }
                 if (!file_exists($val['file'])) {
                     $this->s3_download($val['file'], $s3_bucket, $val['s3_key']);
                 }
             }
             if (self::DEBUG_MODE && function_exists('dbgx_trace_var')) {
                 dbgx_trace_var($S3_medias);
             }
             if (count($S3_medias) > 0 && $S3_medias !== $S3_medias_old) {
                 update_post_meta($attachment->ID, $attachment_meta_key, array($s3_bucket => $S3_medias));
             }
         } else {
             if (!is_admin() || $pagenow === 'upload.php') {
                 foreach ($S3_medias_new as $size => $val) {
                     $search_url = $val['url'];
                     $replace_url = $s3_url . $val['s3_key'];
                     if (!isset($replace_data[$search_url])) {
                         $replace_data[$search_url] = $replace_url;
                         $content = str_replace($search_url, $replace_url, $content);
                     }
                     if (!file_exists($val['file'])) {
                         $this->s3_download($val['file'], $s3_bucket, $val['s3_key']);
                     }
                 }
             }
         }
         $replace_count++;
     }
     if (self::DEBUG_MODE && function_exists('dbgx_trace_var')) {
         dbgx_trace_var($replace_data);
         dbgx_trace_var($replace_data_org);
     }
     if ($post_id && $replace_data !== $replace_data_org) {
         update_post_meta($post_id, $post_meta_key, array($s3_bucket => $replace_data));
     }
     unset($S3_medias_new);
     unset($S3_medias_old);
     unset($S3_medias);
     return $content;
 }
Пример #5
0
 /**
  * Get User Timeline
  * 
  */
 function mobileview_get_user_timeline($username = '', $limit = 5)
 {
     $key = "twitter_user_timeline_{$username}_{$limit}";
     // Check if cache exists
     $timeline = get_transient($key);
     if ($timeline !== false) {
         return $timeline;
     } else {
         $headers = array('Authorization' => 'Bearer ' . $this->mobileview_get_access_token());
         $response = wp_remote_get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={$username}&count={$limit}", array('headers' => $headers, 'timeout' => 40, 'sslverify' => false));
         if (is_wp_error($response)) {
             // In case Twitter is down we return error
             dbgx_trace_var($response);
             return array('error' => __('There is problem fetching twitter timeline', 'colabsthemes'));
         } else {
             // If everything's okay, parse the body and json_decode it
             $json = json_decode(wp_remote_retrieve_body($response));
             // Check for error
             if (!count($json)) {
                 return array('error' => __('There is problem fetching twitter timeline', 'colabsthemes'));
             } elseif (isset($json->errors)) {
                 return array('error' => $json->errors[0]->message);
             } else {
                 set_transient($key, $json, 60 * 60);
                 return $json;
             }
         }
     }
 }
Пример #6
0
 private function _options_update($recv_param)
 {
     $fields = array('head_js', 'remove_js', 'head_filters', 'head_remove', 'foot_filters', 'foot_remove', 'xml_declaration', 'ie_conditional', 'canonical_tag', 'add_ogp_tag', 'ogp_default_image', 'og_type_top', 'og_locale', 'fb_admins', 'fb_app_id', 'add_last_modified', 'foot_js', 'dynamic', 'js_move_foot', 'cache_enabled', 'combined_css', 'css_optimise', 'default_media', 'img_base64', 'combined_js', 'js_minify', 'paranoia_mode', 'gzip_on', 'use_ajax_libs', 'debug_mode', 'wp_generator', 'rsd_link', 'wlwmanifest_link');
     $iv = new InputValidator($recv_param);
     foreach ($fields as $field) {
         switch ($field) {
             case 'head_js':
             case 'remove_js':
             case 'head_filters':
             case 'head_remove':
             case 'foot_filters':
             case 'foot_remove':
                 $iv->set_rules($field, array('trim', 'esc_html', 'required'));
                 break;
             case 'ogp_default_image':
                 //	$iv->set_rules($field, array('trim','esc_html','url'));
                 //	break;
             //	$iv->set_rules($field, array('trim','esc_html','url'));
             //	break;
             case 'og_type_top':
             case 'og_locale':
             case 'fb_admins':
             case 'fb_app_id':
                 $iv->set_rules($field, array('trim', 'esc_html'));
                 break;
             case 'default_media':
                 $iv->set_rules($field, array('trim', 'esc_html'));
                 break;
             default:
                 $iv->set_rules($field, 'bool');
         }
     }
     $options = $iv->input($fields);
     $options['remove_js'] = is_wp_error($options['remove_js']) ? array() : (array) $options['remove_js'];
     unset($iv);
     // head js
     $head_js = is_wp_error($options['head_js']) ? array() : (array) $options['head_js'];
     unset($options['head_js']);
     if (function_exists('dbgx_trace_var')) {
         dbgx_trace_var($head_js);
     }
     foreach ((array) $this->options['head_js'] as $javascript => $value) {
         if (array_search($javascript, $head_js) !== false) {
             $this->options['head_js'][$javascript] = false;
         } else {
             $this->options['head_js'][$javascript] = true;
         }
     }
     unset($head_js);
     // wp_head
     $tag = 'wp_head';
     $all_filters = array_merge((array) $this->options['filters'][$tag], array('wp_generator' => 10, 'rsd_link' => 10, 'wlwmanifest_link' => 10));
     $head_filters = is_wp_error($options['head_filters']) ? array() : (array) $options['head_filters'];
     $head_remove = is_wp_error($options['head_remove']) ? array() : (array) $options['head_remove'];
     unset($options['head_filters']);
     unset($options['head_remove']);
     if (function_exists('dbgx_trace_var')) {
         dbgx_trace_var($head_filters);
         dbgx_trace_var($head_remove);
     }
     foreach ($all_filters as $function_name => $priority) {
         switch ($function_name) {
             case 'wp_generator':
             case 'rsd_link':
             case 'wlwmanifest_link':
                 if ($options[$function_name]) {
                     $this->options['priority'][$tag][$function_name] = -1;
                 } elseif (isset($this->options['priority'][$tag][$function_name])) {
                     unset($this->options['priority'][$tag][$function_name]);
                 }
                 break;
             default:
                 if (array_search($function_name, $head_filters) !== false) {
                     $this->options['priority'][$tag][$function_name] = self::PRIORITY + 1;
                 } elseif (array_search($function_name, $head_remove) !== false) {
                     $this->options['priority'][$tag][$function_name] = -1;
                 } elseif (isset($this->options['priority'][$tag][$function_name])) {
                     unset($this->options['priority'][$tag][$function_name]);
                 }
         }
         if (!$this->function_enabled($function_name) && isset($this->options['filters'][$tag][$function_name])) {
             unset($this->options['filters'][$tag][$function_name]);
         }
     }
     unset($all_filters);
     unset($head_filters);
     unset($head_remove);
     // wp_footer
     $tag = 'wp_footer';
     $all_filters = (array) $this->options['filters'][$tag];
     $foot_filters = is_wp_error($options['foot_filters']) ? array() : (array) $options['foot_filters'];
     $foot_remove = is_wp_error($options['foot_remove']) ? array() : (array) $options['foot_remove'];
     unset($options['foot_filters']);
     unset($options['foot_remove']);
     if (function_exists('dbgx_trace_var')) {
         dbgx_trace_var($foot_filters);
         dbgx_trace_var($foot_remove);
     }
     foreach ($all_filters as $function_name => $priority) {
         if (array_search($function_name, $foot_filters) !== false) {
             $this->options['priority'][$tag][$function_name] = self::PRIORITY + 1;
         } elseif (array_search($function_name, $foot_remove) !== false) {
             $this->options['priority'][$tag][$function_name] = -1;
         } elseif (isset($this->options['priority'][$tag][$function_name])) {
             unset($this->options['priority'][$tag][$function_name]);
         }
         if (!$this->function_enabled($function_name) && isset($this->options['filters'][$tag][$function_name])) {
             unset($this->options['filters'][$tag][$function_name]);
         }
     }
     unset($all_filters);
     unset($foot_filters);
     unset($foot_remove);
     $options_org = $this->options;
     foreach ($options_org as $key => $option) {
         if (isset($options[$key]) && !is_wp_error($options[$key]) && $option !== $options[$key]) {
             $this->options[$key] = $options[$key];
         }
         switch ($key) {
             case 'ogp_default_image':
                 if (is_wp_error($option)) {
                     $this->options[$key] = '';
                 }
                 break;
         }
     }
     if (function_exists('dbgx_trace_var')) {
         dbgx_trace_var($this->options);
     }
     unset($options);
     // options update
     $this->updateOptions();
     // create .htaccess file
     $cache_dir = $this->_create_cache_dir();
     if ($cache_dir !== false) {
         $this->_create_htaccess($cache_dir);
     }
     return $this->options;
 }