Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Main Class
- require_once 'core.php';
- Class WpAutomaticPixabay extends wp_automatic {
- function pixabay_fetch_items($keyword, $camp) {
- echo "<br>so I should now get some images from pixabay for keyword :" . $keyword;
- // $api_key = trim(get_option ( 'wp_automatic_pixabay', '' ));
- $api_key = '4919f28a0f6d35c6ebd388d65bdfe100';
- if (trim($api_key) == '') {
- echo '<br>Pixabay Api key required ';
- exit();
- }
- $camp_opt = unserialize($camp->camp_options);
- if (stristr($camp->camp_general, 'a:')) $camp->camp_general = base64_encode($camp->camp_general);
- $camp_general = unserialize(base64_decode($camp->camp_general));
- $camp_general = array_map('stripslashes', $camp_general);
- $sortby = $camp_general['cg_fl_order'];
- // get start-index for this keyword
- $query = "select keyword_start ,keyword_id from {$this->wp_prefix}automatic_keywords where keyword_name='$keyword' and keyword_camp={$camp->camp_id}";
- $rows = $this->db->get_results($query);
- $row = $rows[0];
- $kid = $row->keyword_id;
- $start = $row->keyword_start;
- if ($start == 0) $start = 1;
- if ($start == - 1) {
- echo '<- exhausted keyword';
- if (!in_array('OPT_FL_CACHE', $camp_opt)) {
- $start =1;
- echo '<br>Cache disabled resetting index to 1';
- } else {
- //check if it is reactivated or still deactivated
- if ($this->is_deactivated($camp->camp_id, $keyword)) {
- $start =1;
- } else {
- //still deactivated
- return false;
- }
- }
- }
- echo ' index:' . $start;
- // update start index to start+1
- $nextstart = $start + 1;
- $query = "update {$this->wp_prefix}automatic_keywords set keyword_start = $nextstart where keyword_id=$kid ";
- $this->db->query($query);
- var_dump($start);
- echo "index buat naik";
- // get items
- $orderby = $camp_general['cg_fl_order'];
- /**
- * liat page=$start
- * nah itu tuh yang bikin naek nya, eh harus nya kaya gitu.
- * pokonya kamu cari deh buat index page nya yang mana
- * jangan di tulis langsung page=1 itu mah terus terus an ngambil page satu
- * sebenernya $start itu gak tau apaan sih di coba coba aja
- */
- $flink = "https://pixabay.com/api/?key=2432254-cf546e7f7f532e93035b97408&image_type=photo&per_page=5&orientation=horizontal&page=$start";
- if (in_array('OPT_FL_USER', $camp_opt)) {
- echo '<br>Fetching images for specific user ' . $camp_general['cg_fl_user'];
- $flink = $flink . '&user_id=' . $camp_general['cg_fl_user'];
- // if keyword *
- if (trim($keyword) == '*') {
- echo '<br>No filtering get all ';
- } else {
- // specific keyword
- $flink = $flink . '&q=' . urlencode($keyword);
- }
- } else {
- // no specific user just text
- $flink = $flink . '&q=' . urlencode($keyword);
- }
- echo '<br>';
- echo $flink;
- echo '<br>';
- // licensing license
- if (in_array('OPT_FL_LICENSE', $camp_opt)) {
- $licenses = array();
- if (in_array('OPT_FL_LICENSE_0', $camp_opt)) $licenses[] = 0;
- if (in_array('OPT_FL_LICENSE_1', $camp_opt)) $licenses[] = 1;
- if (in_array('OPT_FL_LICENSE_2', $camp_opt)) $licenses[] = 2;
- if (in_array('OPT_FL_LICENSE_3', $camp_opt)) $licenses[] = 3;
- if (in_array('OPT_FL_LICENSE_4', $camp_opt)) $licenses[] = 4;
- if (in_array('OPT_FL_LICENSE_5', $camp_opt)) $licenses[] = 5;
- if (in_array('OPT_FL_LICENSE_6', $camp_opt)) $licenses[] = 6;
- if (in_array('OPT_FL_LICENSE_7', $camp_opt)) $licenses[] = 7;
- if (in_array('OPT_FL_LICENSE_8', $camp_opt)) $licenses[] = 8;
- if (in_array('OPT_FL_LICENSE_9', $camp_opt)) $licenses[] = 9;
- if (count($licenses) > 0) $flink .= "&license=" . implode(',', $licenses);
- }
- // curl get
- $x = 'error';
- $url = $flink;
- curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
- curl_setopt($this->ch, CURLOPT_URL, trim($url));
- while (trim($x) != '') {
- $exec = curl_exec($this->ch);
- $x = curl_error($this->ch);
- }
- $results = json_decode($exec, true);
- // foreach ($results as $k => $v) {
- // echo "<pre>".print_r($v, 1)."</pre>";
- // echo $v->urls->raw;
- // echo "<br>";
- // }
- // die();
- if (is_array($results)) {
- echo '<br>Valid array returned from pixabay ';
- // $imgs = $results['user']['urls'];
- $imgs = array();
- foreach ($results['hits'] as $result ){
- array_push($imgs, array(
- 'user' => $result['user'],
- 'urls' => $result['webformatURL'],
- 'id' => $result['id'],
- 'title' => $keyword . ' by ' . $result['user'],
- 'likes' => $result['likes'],
- 'author' => $result['userImageURL'],
- 'links' => $result['pageURL'],
- 'tags' => $result['tags'],
- ));
- // echo "result";
- // echo '<pre>';
- // var_dump($imgs);
- // echo '</pre>';
- // array_push($imgs, $result['id']);
- }
- if (is_array($imgs)) {
- echo '<br>Valid reply array returned with ' . count($imgs) . ' child';
- if (count($imgs) == 0) {
- echo '<br>Keyword have no more images deactivating...';
- $query = "update {$this->wp_prefix}automatic_keywords set keyword_start = -1 where keyword_id=$kid ";
- $this->db->query($query);
- $this->deactivate_key($camp->camp_id, $keyword);
- }
- } else {
- echo '<br>Did not find valid image array in the response ';
- $imgs = array();
- }
- } else {
- echo '<br>Pixabay did not reuturn valid reply array ';
- }
- echo '<ol>';
- foreach ($imgs as $itm) {
- $id = $itm['id'];
- $data = serialize($itm);
- $item_link = "https://pixabay.com/api/?key=2432254-cf546e7f7f532e93035b97408&image_type=photo&per_page=200&orientation=horizontal&$id";
- // JIKA DI HILANGI MAKA AKAN MUNCUL DUPLIKAT ID
- echo '<li> Link: '.$item_link;
- if ($this->is_execluded($camp->camp_id, $item_link)) {
- echo '<-- Execluded';
- continue;
- }
- if (!$this->is_duplicate($item_link)) {
- $query = "INSERT INTO {$this->wp_prefix}automatic_general ( item_id , item_status , item_data ,item_type) values ( '$id', '0', '$data' ,'fl_{$camp->camp_id}_$keyword' )";
- $this->db->query($query);
- } else {
- echo ' <- duplicated <a href="' . get_edit_post_link($this->duplicate_id) . '">#' . $this->duplicate_id . '</a>';
- }
- }
- echo '</ol>';
- }
- /*
- * ---* pixabay post ---
- */
- function pixabay_get_post($camp) {
- // $api_key = get_option ( 'wp_automatic_pixabay', '' );
- $api_key = '4919f28a0f6d35c6ebd388d65bdfe100';
- if (trim($api_key) == '') {
- echo '<br>Pixabay Api key required visit settings and add it ';
- exit();
- }
- $camp_opt = unserialize($camp->camp_options);
- $keywords = explode(',', $camp->camp_keywords);
- foreach ($keywords as $keyword) {
- $keyword = trim($keyword);
- //update last keyword
- update_post_meta($camp->camp_id, 'last_keyword', trim($keyword));
- if (trim($keyword) != '') {
- // getting links from the db for that keyword
- $query = "select * from {$this->wp_prefix}automatic_general where item_type= 'fl_{$camp->camp_id}_$keyword' and item_status ='0'";
- $this->used_keyword = $keyword;
- $res = $this->db->get_results($query);
- // when no links lets get new links
- if (count ( $res ) == 0) {
- $this->pixabay_fetch_items($keyword, $camp);
- // getting links from the db for that keyword
- $res = $this->db->get_results($query);
- }
- // echo '<pre>';
- // var_dump($res);
- // echo '<pre>';
- //check if already duplicated
- //deleting duplicated items
- $res_count = count($res);
- for ($i = 0; $i < $res_count; $i++) {
- $t_row = $res[$i];
- $t_data = unserialize($t_row->item_data);
- $id = $t_data['id'];
- // echo 'tdataaaaa';
- // echo '<pre>';
- // var_dump($t_data);
- // echo '<pre>';
- $t_link_url = "https://pixabay.com/api/?key=2432254-cf546e7f7f532e93035b97408&image_type=photo&per_page=200&orientation=horizontal&$id";
- if ($this->is_duplicate($t_link_url)) {
- echo 'duplicated';
- // die();
- //duplicated item let's delete
- unset($res[$i]);
- echo '<br>Pixabay image (' . $t_data['title'] . ') found cached but duplicated <a href="' . get_permalink($this->duplicate_id) . '">#' . $this->duplicate_id . '</a>' ;
- //delete the item
- $query = "delete from {$this->wp_prefix}automatic_general where item_id='{$t_row->item_id}' and item_type= 'fl_{$camp->camp_id}_$keyword'";
- $this->db->query($query);
- } else {
- break;
- }
- }
- // check again if valid links found for that keyword otherwise skip it
- if (count($res) > 0) {
- // lets process that link
- $ret = $res[$i];
- $data = unserialize($ret->item_data);
- $temp['img_title'] = $data['title'];
- $temp['img_author'] = $data['user'];
- $temp['profile_author'] = $data['author'];
- // $temp['img_src'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}.jpg";
- // $temp['img_src_s'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_s.jpg";
- // $temp['img_src_q'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_q.jpg";
- // $temp['img_src_t'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_t.jpg";
- // $temp['img_src_m'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_m.jpg";
- // $temp['img_src_n'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_n.jpg";
- // $temp['img_src_z'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_z.jpg";
- // $temp['img_src_c'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_c.jpg";
- // $temp['img_src_b'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_b.jpg";
- // $temp['img_link'] = 'http://unsplash.com/' . $data ['owner'] . '/' . $ret->item_id;
- $temp['img_likes'] = $data['likes'];
- $temp['img_tags'] = $data['tags'];
- $temp['img_src'] = $data['urls'];
- $temp['img_link'] = $data['links'];
- $temp['img_categories'] = $data['tags'];
- $temp['img_download'] = $data['urls'];
- echo '<br>Found Link:<a href="' . $temp['img_link'] . '">' . $temp['img_title'] . '</a>';
- // getting photo description
- // curl get
- // $x = 'error';
- // $url = "https://api.unsplash.com/photos/{$ret->item_id}?client_id=efb65c79976543a262d51c13 c393c1d59881bce5e17f10c33b4fc0176909cbea";
- // curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
- // curl_setopt($this->ch, CURLOPT_URL, trim($url));
- // while (trim($x) != '') {
- // $exec = curl_exec($this->ch);
- // $x = curl_error($this->ch);
- // }
- // echo $url;
- // $exec = unserialize($exec);
- // if (!is_array($exec)) {
- // echo '<br> Not valid array ';
- // } else {
- // $temp['img_author_name'] = $exec['photo']['owner']['username'];
- // $temp['img_description'] = $exec['photo']['description']['_content'];
- // $temp['img_date_posted'] = date('Y-m-d H:i:s', $exec['photo']['dates']['posted']);
- // $temp['img_date_taken'] = $exec['photo']['dates']['taken'];
- // $temp['img_viewed'] = $exec['photo']['views'];
- // $tags = '';
- // foreach ($exec['photo']['tags']['tag'] as $tag) {
- // $tags = $tags . ' , ' . $tag['raw'];
- // }
- // $temp['img_tags'] = $tags;
- // }
- // update the link status to 1
- $query = "update {$this->wp_prefix}automatic_general set item_status='1' where item_id='$ret->item_id' and item_type='fl_{$camp->camp_id}_$keyword' ";
- $this->db->query($query);
- // if cache not active let's delete the cached videos and reset indexes
- if (!in_array('OPT_FL_CACHE', $camp_opt)) {
- echo '<br>Cache disabled claring cache ...';
- $query = "delete from {$this->wp_prefix}automatic_general where item_type='fl_{$camp->camp_id}_$keyword' and item_status ='0'";
- $this->db->query($query);
- // reset index
- $query = "update {$this->wp_prefix}automatic_keywords set keyword_start =1 where keyword_camp={$camp->camp_id}";
- $this->db->query($query);
- }
- return $temp;
- } else {
- echo '<br>No links found for this keyword';
- }
- } // if trim
- } // foreach keyword
- }
- }
Add Comment
Please, Sign In to add comment