Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ImageGrabber {
- private $use_proxy = FALSE;
- private $proxy;
- private $proxy_username;
- private $proxy_password;
- public function __construct() {
- register_activation_hook(__FILE__, array(&$this, 'activate'));
- add_action('admin_menu', array(&$this, 'adminMenu'));
- add_filter("media_upload_tabs", array(&$this, "build_tab"));
- add_action("media_upload_ImageGrabber", array(&$this, "menu_handle"));
- $this->use_proxy = (bool)get_option('image-grab.use_proxy');
- $this->proxy = get_option('image-grab.proxy_host') . ':' . (int)get_option('image-grab.proxy_port');
- $this->proxy_username = get_option('image-grab.proxy_username');
- $this->proxy_password = get_option('image-grab.proxy_password');
- add_action('admin_enqueue_scripts', array(&$this, 'image_grabber_style'));
- add_action('wp_ajax_add_category', array(&$this, 'add_category_callback'));
- }
- function image_grabber_style() {
- wp_enqueue_style('style_css', plugin_dir_url(__FILE__) . 'style.css');
- }
- function add_category_callback() {
- $action = $_POST['action'];
- $taxonomy = get_taxonomy(substr($action, 4));
- check_ajax_referer($action, '_ajax_nonce-add-' . $taxonomy->name);
- if (!current_user_can($taxonomy->cap->edit_terms)) wp_die(-1);
- $names = explode(',', $_POST['new' . $taxonomy->name]);
- $parent = isset($_POST['new' . $taxonomy->name . '_parent']) ? (int)$_POST['new' . $taxonomy->name . '_parent'] : 0;
- if (0 > $parent) $parent = 0;
- if ($taxonomy->name == 'category') $post_category = isset($_POST['post_category']) ? (array)$_POST['post_category'] : array();
- else $post_category = (isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name])) ? (array)$_POST['tax_input'][$taxonomy->name] : array();
- $checked_categories = array_map('absint', (array)$post_category);
- foreach ($names as $cat_name) {
- $cat_name = trim($cat_name);
- $category_nicename = sanitize_title($cat_name);
- if ('' === $category_nicename) continue;
- if (!$cat_id = term_exists($cat_name, $taxonomy->name, $parent)) $cat_id = wp_insert_term($cat_name, $taxonomy->name, array('parent' => $parent));
- if (is_wp_error($cat_id)) continue;
- elseif (is_array($cat_id)) $cat_id = $cat_id['term_id'];
- $checked_categories[] = $cat_id;
- if ($parent) continue;
- }
- if ($parent) {
- $parent = get_term($parent, $taxonomy->name);
- $term_id = $parent->term_id;
- while ($parent->parent) {
- $parent = get_term($parent->parent, $taxonomy->name);
- if (is_wp_error($parent)) break;
- $term_id = $parent->term_id;
- }
- }
- $combo_category = $this->_buildCategoryOptionList('post_category[]', $checked_categories, 'post_category', get_option('image-grab.multiple_category'));
- ob_start();
- wp_dropdown_categories(array('taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new' . $taxonomy->name . '_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => '— ' . $taxonomy->labels->parent_item . ' —'));
- $combo_add_category = ob_get_contents();
- ob_end_clean();
- $response = '<div id="response">';
- $response.= '<div id="category_list">' . $combo_category . '</div>';
- $response.= '<div id="new_category_list">' . $combo_add_category . '</div>';
- $response.= '</div>';
- echo $response;
- die();
- }
- function activate() {
- add_option('image-grab.default_category', serialize(array(0)));
- add_option('image-grab.multiple_category', '0');
- add_option('image-grab.schedule_interval', '60');
- add_option('image-grab.content_desc', "New Wallpaper {TITLE} Dimesion: {DIMENSION} File Size: {FILE_SIZE}");
- }
- private function array_insert(&$array, $insert, $position) {
- settype($array, "array");
- settype($insert, "array");
- settype($position, "int");
- if ($position == 0) {
- $array = array_merge($insert, $array);
- } else {
- if ($position >= (count($array) - 1)) {
- $array = array_merge($array, $insert);
- } else {
- $head = array_slice($array, 0, $position);
- $tail = array_slice($array, $position);
- $array = array_merge($head, $insert, $tail);
- }
- }
- return $array;
- }
- public function build_tab($tabs) {
- $newtab = array('ImageGrabber' => __('Grab Images', 'ImageGrabber'));
- return $this->array_insert($tabs, $newtab, 2);
- }
- public function menu_handle() {
- return wp_iframe(array(&$this, "media_process"));
- }
- private function fetch_image($url) {
- if (function_exists("curl_init")) {
- return $this->curl_fetch_image($url);
- } elseif (ini_get("allow_url_fopen")) {
- return $this->fopen_fetch_image($url);
- }
- }
- private function curl_fetch_image($url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $image = curl_exec($ch);
- curl_close($ch);
- return $image;
- }
- private function fopen_fetch_image($url) {
- $image = file_get_contents($url, FALSE, $context);
- return $image;
- ................................................................
- .................................
- ........
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement