Bei Aktivierung dieses Plugins wird automatisch eine Seite mit Hotelbewertungen und Angeboten generiert.
Author: ab-in-den-urlaub
Version: 1.2
*/
// default settings
define('CONTENT_4_PARTNERS_VERSION', '1.2');
define('CONTENT_4_PARTNERS_HOST', 'webservice.ab-in-den-urlaub.de');
define('CONTENT_4_PARTNERS_PORT', '80');
define('CONTENT_4_PARTNERS_DIR', '');
define('CONTENT_4_PARTNERS_USER', '');
define('CONTENT_4_PARTNERS_PASSWORD', '');
define('CONTENT_4_PARTNERS_DEFAULT_ID', '');
define('CONTENT_4_PARTNERS_CACHE_LIFETIME', 3600);
define('CONTENT_4_PARTNERS_CACHE_USE', '1');
define('CONTENT_4_PARTNERS_NAMESPACE', 'Partner');
// max cache lifetime in seconds (one day = 86400 seconds)
define('CONTENT_4_PARTNERS_CACHE_MAX_LIFETIME', 86400);
// default plugin setting names
define('CONTENT_4_PARTNERS_SETTINGS_GROUP', 'content_4_partners_settings_options');
define('CONTENT_4_PARTNERS_SETTINGS', 'content_4_partners_settings');
// registration data for plugin
define('CONTENT_4_PARTNERS_REGDATA_GROUP', 'content_4_partners_regdata_options');
define('CONTENT_4_PARTNERS_REGDATA', 'content_4_partners_regdata');
// default cache settings
define('CONTENT_4_PARTNERS_DEFAULT_CACHE_LIFETIME', 3600);
define('CONTENT_4_PARTNERS_CACHE_KEY_PREFIX', 'content_4_partners_');
// helper for finding this plugin when links are added to plugin actions
static $content4partners_plugin;
/**
* Content 4 Partners Plugin.
*/
class Content4Partners
{
// cached options, always get options via $this->getOptions() !!!!
var $options = null;
/**
* Creates new content 4 partners plugin.
*/
function Content4Partners()
{
// register admin hooks
add_action('admin_init', array(&$this, 'hookAdminInit'));
add_action('admin_menu', array(&$this, 'hookAdminMenu'));
// register activation/deactivation hooks
register_activation_hook(__FILE__, array(&$this, 'hookAdminPluginActivate'));
register_deactivation_hook(__FILE__, array(&$this, 'hookAdminPluginDeactivate'));
// register short cuts
add_shortcode('content4partners', array(&$this, 'shortCodeContent4Partners'));
// register plugin action hooks
add_filter('plugin_action_links', array(&$this, 'hookAddPluginAction'), 10, 2);
// register title hook
add_filter('wp_title', array(&$this, 'hookWpTitle'), 999);
remove_filter('the_content', 'wpautop');
add_filter('the_content', array(&$this, 'wpautopnobr'));
// to manipulate the behavior of the wpseo-plugin
// user can decide if he wants the wpseo to manipulate the title as well
if(1 == get_post_meta($this->getPageId(), '_wpseo_edit_only', true)){
$optionsseo = get_option('wpseo_options');
// if there are no wpseo_options the wpseo-plugin may not installed
if(false === $optionsseo){
update_post_meta($this->getPageId(), '_wpseo_edit_title', trim($this->getTitleForId($this->getRegionId())) . ' | ' . get_bloginfo());
}
else{
update_post_meta($this->getPageId(), '_wpseo_edit_title', trim($this->getTitleForId($this->getRegionId())) . ' ' . $optionsseo['wp_seo_title_separator'] . ' ' . get_bloginfo());
}
}
}
/**
* do not insert
-Tags
* @param unknown_type $content
*/
function wpautopnobr($content)
{
return wpautop($content, false);
}
/**
* This method is called when the shortcode [content4partners] is
* found in any content.
*
* @param $attributes
* @param $content
*/
function shortCodeContent4Partners($attributes, $content = null)
{
$id = $this->getRegionId($attributes);
// check again
if (is_null($id)) {
return "Keine Region ID angegeben.";
} else {
return $this->getContent($id);
}
}
/**
* Gets the content for given region id.
*
* @param $id the region id
*/
function getContent($id)
{
$useCache = $this->getCacheUse();
$key = CONTENT_4_PARTNERS_CACHE_KEY_PREFIX . $id;
if ($useCache) {
$content = get_transient($key);
if ($content !== false && !empty($content)) {
return $content;
}
}
$content = $this->getContentFromRegionId($id);
if ($useCache && !empty($content)) {
$cacheLifetime = $this->getCacheLifetime();
set_transient($key, $content, $cacheLifetime);
}
if (empty($content)) {
$content = "Fehler beim Abrufen der Daten !";
}
return $content;
}
/**
* Grabs content for given url.
*
* @param $url
* @return
*/
function getContentFromRegionId($regionId, $action = null)
{
$options = $this->getOptions();
if (CONTENT_4_PARTNERS_HOST == '') {
// no valid host
return '';
}
if (!isset($action)) {
$action = $options['action'];
}
$content = '';
$url = 'tcp://' . CONTENT_4_PARTNERS_HOST;
$fp = fsockopen($url, CONTENT_4_PARTNERS_PORT, $errno, $errstr, 30);
if (!$fp) {
// TODO error handling ?
return $content;
} else {
$get = CONTENT_4_PARTNERS_DIR . '/xml.php?namespace=' . $options['namespace']
. '&action=' . $action . '¶m[]=' . $options['user'] . '¶m[]='
. $options['password'] . '¶m[]=' . $regionId;
if($action == 'getPage'){
$get.= '¶m[]='.CONTENT_4_PARTNERS_VERSION;
}
fwrite($fp, "GET " . $get . " HTTP/1.0\r\nHost: " . CONTENT_4_PARTNERS_HOST . "\r\nAccept: */*\r\n\r\n");
$started = false;
while (!feof($fp)) {
$line = trim(fgets($fp, 1024));
if ($started) {
$content .= $line . "\r\n";
} elseif (strlen($line) == 0) {
$started = true;
}
}
fclose($fp);
// replace placeholders with page id
$pageId = $this->getPageId();
if ($pageId !== false) {
$content = preg_replace('|##page_id##|', $pageId, $content);
}
}
return $content;
}
/**
* Gets plugin options.
*
* @return plugin options
*/
function getOptions()
{
if (!isset($this->options)) {
$this->options = get_option(CONTENT_4_PARTNERS_SETTINGS);
}
return $this->options;
}
/**
* Gets lifetime of cache.
*
* @return cache lifetime
*/
function getCacheLifetime()
{
$options = $this->getOptions();
return intval($options['cache_lifetime']);
}
/**
* Returns true if cache is enabled otherwise false.
*
* @return true if cache is enabled
*/
function getCacheUse()
{
$options = $this->getOptions();
if (array_key_exists('cache_use', $options)) {
$useCache = $options['cache_use'] == '1' ? true : false;
}
return $useCache;
}
/**
* Gets the region id, will check GET and POST and params array if supplied.
*
* @return region id
*/
function getRegionId($params = null)
{
$id = null;
// check if parameter id is within get parameters
if (array_key_exists('acfpid', $_GET)) {
$id = $_GET['acfpid'];
} elseif (array_key_exists('acfpid', $_POST)) {
// check if parameter id is within post parameters
$id = $_POST['acfpid'];
} elseif (isset($params) && is_array($params) && array_key_exists('acfpid', $params)) {
// check if parameter id is in attributes of shortcode
$id = $params['acfpid'];
}
// check for default region id
if (empty($id)) {
$options = $this->getOptions();
$id = $options['acfpid'];
}
if (empty($id)) {
$id = null;
}
return $id;
}
/**
* Gets page id.
*
* @return page id
*/
function getPageId()
{
$id = false;
$currentPage = get_page_by_title(get_the_title());
if (!is_null($currentPage)) {
$id = $currentPage->ID;
}
if ($id === false) {
$options = $this->getOptions();
if (array_key_exists('page_id', $options)) {
$id = intval($options['page_id']);
}
}
return $id;
}
/**
* Gets the title for given region id.
*
* @param $id region id
* @return title of region
*/
function getTitleForId($id)
{
return $this->getContentFromRegionId($id, 'getPageTitle');
}
/**
* Flushs cache.
*/
function flushCache()
{
global $wpdb;
// delete entries from options table straight by sql query
// cache keys match pattern "_transient*content_4_partners_$id"
$query = "
DELETE from "
. $wpdb->options . "
WHERE option_name like '%_transient%%content_4_partners_%'";
$wpdb->query($query);
}
/**
* get partner by login and password
*
* returns true when login exists or false on error
* if the result is false, the login is failed
*
* @param string $login
* @param string $password
*
* @return boolean
*/
function loginExists($login, $password)
{
global $wpdb;
$action = 'login';
$options = $this->getOptions();
$loginResult = '';
$url = 'tcp://' . CONTENT_4_PARTNERS_HOST;
$fp = fsockopen($url, CONTENT_4_PARTNERS_PORT, $errno, $errstr, 30);
if (!$fp) {
// TODO error handling ?
return false;
} else{
$get = CONTENT_4_PARTNERS_DIR . '/xml.php?namespace=' . $options['namespace']
. '&action=' . $action . '¶m[]=' . $login . '¶m[]=' . $password . '&NoCache=1';
fwrite($fp, "GET " . $get . " HTTP/1.0\r\nHost: " . CONTENT_4_PARTNERS_HOST . "\r\nAccept: */*\r\n\r\n");
$started = false;
while (!feof($fp)) {
$line = trim(fgets($fp, 1024));
if ($started) {
$loginResult .= $line . "\r\n";
} elseif (strlen($line) == 0) {
$started = true;
}
}
fclose($fp);
}
if (strpos($loginResult, '
C4PP holds an automatic generated hotel page.
weitere Funktionen |