public function publish($title, $content, $author, $image, $category = 1, $publish_type = 'draft') { $listCats = []; if (is_array($category)) { foreach ($category as $key => $value) { $listCats[] = $value; } } $post = ['post_title' => $title, 'post_content' => $content, 'post_category' => $listCats, 'post_status' => $publish_type, 'post_author' => $author]; //temporarily disable remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', 'wp_filter_post_kses'); try { $newPostId = wp_insert_post($post); } catch (Exception $e) { logger::alert($e->getMessage()); return false; } if ($newPostId == false) { return false; } // attach image to post if $image is not empty if ($image != '') { $filetype = wp_check_filetype(basename($image), null); $wp_upload_dir = wp_upload_dir(); $attachment = ['guid' => $wp_upload_dir['url'] . '/' . basename($image), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($image)), 'post_content' => '', 'post_status' => 'inherit']; $attach_id = wp_insert_attachment($attachment, $image, $attachment); $attach_data = wp_generate_attachment_metadata($attach_id, $image); wp_update_attachment_metadata($attach_id, $attach_data); set_post_thumbnail($newPostId, $attach_id); } //bring it back once you're done posting add_filter('content_save_pre', 'wp_filter_post_kses'); add_filter('content_filtered_save_pre', 'wp_filter_post_kses'); return $newPostId; }
/** * @return void */ public function continueWithFoundLinks() { //reset DB in case this is executed in a forked child process $this->db = Pimcore_Resource_Mysql::reset(); try { $row = $this->db->fetchRow("SELECT * FROM plugin_searchphp_frontend_crawler_todo ORDER BY id", array()); $nextLink = $row['uri']; $depth = $row['depth']; $cookieJar = unserialize($row['cookiejar']); } catch (Exception $e) { // probably table was already removed because crawler is finished logger::log(get_class($this) . ": Could not extract next link from table plugin_searchphp_frontend_crawler_todo ", Zend_Log::DEBUG); return; } if (empty($nextLink)) { return; } $client = Pimcore_Tool::getHttpClient(); $client->setUri($nextLink); $client->setConfig(array('maxredirects' => $this->maxRedirects, 'keepalive' => true, 'timeout' => $this->timeout)); $client->setCookieJar($cookieJar); $client->setHeaders('If-Modified-Since', null); while ($nextLink) { try { $this->db->delete("plugin_searchphp_frontend_crawler_todo", "id = '" . md5($nextLink) . "'"); } catch (Exception $e) { logger::warn(get_class($this) . ": Could not delete from plugin_searchphp_frontend_crawler_todo - maybe forcing crawler stop right now?"); } if ($depth <= $this->maxLinkDepth) { logger::debug(get_class($this) . ": Link depth [ {$depth} ]"); try { $nextLink = $this->addEvictOutputFilterParameter($nextLink); $client->setUri($nextLink); $client->setCookieJar($cookieJar); $client->setHeaders('If-Modified-Since', null); try { $response = $client->request(); } catch (Zend_Http_Client_Adapter_Exception $e) { logger::log(get_class($this) . ": Could not get response for Link [ {$nextLink} ] ", Zend_Log::ERR); } if ($response instanceof Zend_Http_Response and ($response->isSuccessful() or $response->isRedirect())) { //we don't use port - crawler ist limited to standard port 80 $client->getUri()->setPort(null); //update url - maybe we were redirected $nextLink = $client->getUri(true); $nextLink = $this->removeOutputFilterParameters($nextLink); $valid = $this->validateLink($nextLink); if ($valid) { //see if we were redirected to a place we already have in fetch list or done try { $rowTodo = $this->db->fetchRow("SELECT count(*) as count from plugin_searchphp_frontend_crawler_todo WHERE id ='" . md5($nextLink) . "'"); } catch (Exception $e) { logger::log(get_class($this) . ": could not fetch from plugin_searchphp_contents_temp", Zend_Log::DEBUG); } try { $rowDone = $this->db->fetchRow("SELECT count(*) as count from plugin_searchphp_contents_temp WHERE id ='" . md5($nextLink) . "'"); } catch (Exception $e) { logger::log(get_class($this) . ": could not fetch from plugin_searchphp_contents_temp", Zend_Log::DEBUG); } try { $rowNoIndex = $this->db->fetchRow("SELECT count(*) as count from plugin_searchphp_frontend_crawler_noindex WHERE id ='" . md5($nextLink) . "'"); } catch (Exception $e) { logger::log(get_class($this) . ": could not fetch from plugin_searchphp_frontend_crawler_noindex", Zend_Log::DEBUG); } if ($rowTodo['count'] > 0 or $rowDone['count'] > 0 or $rowNoIndex['count'] > 0) { logger::log(get_class($this) . " Redirected to uri [ {$nextLink} ] - which has already been processed", Zend_Log::DEBUG); } else { try { $success = $this->parse($nextLink, $response, $client->getUri()->getHost(), $client->getCookieJar(), $depth); logger::log(get_class($this) . ": parsed [ {$nextLink} ] ", Zend_Log::DEBUG); } catch (Exception $e) { logger::log($e, Zend_Log::ERR); } } } else { logger::log("We were redirected to an invalid Link [ {$nextLink}]", Zend_Log::DEBUG); } } else { logger::log(get_class($this) . ": Error parsing [ {$nextLink} ] ", Zend_Log::ERR); } } catch (Zend_Uri_Exception $e) { logger::log(get_class($this) . ": Invalid URI [ {$nextLink} ] ", Zend_Log::ERR); } } else { logger::alert(get_class($this) . ": Stopping with uri [ {$nextLink} ] because maximum link depth of [ {$depth} ] has been reached."); } //get next from DB try { $row = $this->db->fetchRow("SELECT * FROM plugin_searchphp_frontend_crawler_todo ORDER BY id", array()); $nextLink = $row['uri']; $depth = $row['depth']; $cookieJar = unserialize($row['cookiejar']); } catch (Exception $e) { //wait 2 seconds then try again sleep(2); try { $row = $this->db->fetchRow("SELECT * FROM plugin_searchphp_frontend_crawler_todo ORDER BY id", array()); $nextLink = $row['uri']; $depth = $row['depth']; $cookieJar = unserialize($row['cookiejar']); } catch (Exception $e) { // probably table was already removed because crawler is finished logger::log(get_class($this) . ": Could not extract next link from table plugin_searchphp_frontend_crawler_todo ", Zend_Log::DEBUG); $nextLink = false; } } } }