Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. <?php
  2. class ImageGrabber {
  3. private $use_proxy = FALSE;
  4. private $proxy;
  5. private $proxy_username;
  6. private $proxy_password;
  7. public function __construct() {
  8. register_activation_hook(__FILE__, array(&$this, 'activate'));
  9. add_action('admin_menu', array(&$this, 'adminMenu'));
  10. add_filter("media_upload_tabs", array(&$this, "build_tab"));
  11. add_action("media_upload_ImageGrabber", array(&$this, "menu_handle"));
  12. $this->use_proxy = (bool)get_option('image-grab.use_proxy');
  13. $this->proxy = get_option('image-grab.proxy_host') . ':' . (int)get_option('image-grab.proxy_port');
  14. $this->proxy_username = get_option('image-grab.proxy_username');
  15. $this->proxy_password = get_option('image-grab.proxy_password');
  16. add_action('admin_enqueue_scripts', array(&$this, 'image_grabber_style'));
  17. add_action('wp_ajax_add_category', array(&$this, 'add_category_callback'));
  18. }
  19. function image_grabber_style() {
  20. wp_enqueue_style('style_css', plugin_dir_url(__FILE__) . 'style.css');
  21. }
  22. function add_category_callback() {
  23. $action = $_POST['action'];
  24. $taxonomy = get_taxonomy(substr($action, 4));
  25. check_ajax_referer($action, '_ajax_nonce-add-' . $taxonomy->name);
  26. if (!current_user_can($taxonomy->cap->edit_terms)) wp_die(-1);
  27. $names = explode(',', $_POST['new' . $taxonomy->name]);
  28. $parent = isset($_POST['new' . $taxonomy->name . '_parent']) ? (int)$_POST['new' . $taxonomy->name . '_parent'] : 0;
  29. if (0 > $parent) $parent = 0;
  30. if ($taxonomy->name == 'category') $post_category = isset($_POST['post_category']) ? (array)$_POST['post_category'] : array();
  31. else $post_category = (isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name])) ? (array)$_POST['tax_input'][$taxonomy->name] : array();
  32. $checked_categories = array_map('absint', (array)$post_category);
  33. foreach ($names as $cat_name) {
  34. $cat_name = trim($cat_name);
  35. $category_nicename = sanitize_title($cat_name);
  36. if ('' === $category_nicename) continue;
  37. if (!$cat_id = term_exists($cat_name, $taxonomy->name, $parent)) $cat_id = wp_insert_term($cat_name, $taxonomy->name, array('parent' => $parent));
  38. if (is_wp_error($cat_id)) continue;
  39. elseif (is_array($cat_id)) $cat_id = $cat_id['term_id'];
  40. $checked_categories[] = $cat_id;
  41. if ($parent) continue;
  42. }
  43. if ($parent) {
  44. $parent = get_term($parent, $taxonomy->name);
  45. $term_id = $parent->term_id;
  46. while ($parent->parent) {
  47. $parent = get_term($parent->parent, $taxonomy->name);
  48. if (is_wp_error($parent)) break;
  49. $term_id = $parent->term_id;
  50. }
  51. }
  52. $combo_category = $this->_buildCategoryOptionList('post_category[]', $checked_categories, 'post_category', get_option('image-grab.multiple_category'));
  53. ob_start();
  54. wp_dropdown_categories(array('taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new' . $taxonomy->name . '_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => '&mdash; ' . $taxonomy->labels->parent_item . ' &mdash;'));
  55. $combo_add_category = ob_get_contents();
  56. ob_end_clean();
  57. $response = '<div id="response">';
  58. $response.= '<div id="category_list">' . $combo_category . '</div>';
  59. $response.= '<div id="new_category_list">' . $combo_add_category . '</div>';
  60. $response.= '</div>';
  61. echo $response;
  62. die();
  63. }
  64. function activate() {
  65. add_option('image-grab.default_category', serialize(array(0)));
  66. add_option('image-grab.multiple_category', '0');
  67. add_option('image-grab.schedule_interval', '60');
  68. add_option('image-grab.content_desc', "New Wallpaper {TITLE} Dimesion: {DIMENSION} File Size: {FILE_SIZE}");
  69. }
  70. private function array_insert(&$array, $insert, $position) {
  71. settype($array, "array");
  72. settype($insert, "array");
  73. settype($position, "int");
  74. if ($position == 0) {
  75. $array = array_merge($insert, $array);
  76. } else {
  77. if ($position >= (count($array) - 1)) {
  78. $array = array_merge($array, $insert);
  79. } else {
  80. $head = array_slice($array, 0, $position);
  81. $tail = array_slice($array, $position);
  82. $array = array_merge($head, $insert, $tail);
  83. }
  84. }
  85. return $array;
  86. }
  87. public function build_tab($tabs) {
  88. $newtab = array('ImageGrabber' => __('Grab Images', 'ImageGrabber'));
  89. return $this->array_insert($tabs, $newtab, 2);
  90. }
  91. public function menu_handle() {
  92. return wp_iframe(array(&$this, "media_process"));
  93. }
  94. private function fetch_image($url) {
  95. if (function_exists("curl_init")) {
  96. return $this->curl_fetch_image($url);
  97. } elseif (ini_get("allow_url_fopen")) {
  98. return $this->fopen_fetch_image($url);
  99. }
  100. }
  101. private function curl_fetch_image($url) {
  102. $ch = curl_init();
  103. curl_setopt($ch, CURLOPT_URL, $url);
  104. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  105. $image = curl_exec($ch);
  106. curl_close($ch);
  107. return $image;
  108. }
  109. private function fopen_fetch_image($url) {
  110. $image = file_get_contents($url, FALSE, $context);
  111. return $image;
  112. ................................................................
  113. .................................
  114. ........
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement