optionsGetOptions();
}
if (( isset($options)) && ($options['mobilify_optin'] )) {
add_action( 'admin_init', array( &$this, 'ahalogyMobileCheck' ) ); // mobile reqs check
add_action( 'save_post', array( &$this, 'updateMobilePost' ) );
add_action( 'template_redirect', array(&$this, 'jsonTemplateRedirect'));
add_action( 'wp_head', array( &$this, 'getMobilifyHeaderCode' ), 99999 );
}
}
function getMobilifyHeaderCode() {
global $ahalogyWP_instance;
global $post;
if (is_single()) {
$options = $ahalogyWP_instance->optionsGetOptions();
if ($ahalogyWP_instance->display_none) {
$css_rule = '';
} else {
$css_rule = '';
}
// Mobilify widget code
$widget_code = sprintf( '
%2$s
'
,
$ahalogyWP_instance->plugin_version
,
$css_rule
,
$ahalogyWP_instance->mobilify_js_domain
,
$options['client_id']
,
$post->ID
);
// build code
if( $options['insert_code'] && strlen($options['client_id'])>=10)
echo $widget_code;
}
}
// Ping ahalogy when a post when it is saved or updated. Update the post meta field 'ahalogy_response' when their response
function updateMobilePost( $post_id ) {
global $ahalogyWP_instance;
$options = $ahalogyWP_instance->optionsGetOptions();
$slug = 'post';
// If this isn't a 'post' post, don't update it.
if(isset($_POST['post_type'])) {
if ( $slug != $_POST['post_type'] ) {
return;
}
// If this is just a revision, don't do anything
if ( wp_is_post_revision( $post_id ) ) {
return;
}
//Here is where we will ping ahalogy's servers with new post information.
if ($options['client_id']) {
$url = $ahalogyWP_instance->mobilify_api_domain . '/api/articles/notify_changed/' . $options['client_id'] . '/' . $post_id;
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 10,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array( 'url' => get_permalink($post_id), 'modified_at' => get_post_modified_time($ahalogyWP_instance->date_format, true, $post_id) ),
'cookies' => array()
)
);
if ( is_wp_error( $response ) ) {
//Setting the error but we won't do anything with it for now.
$error_message = $response->get_error_message();
//print_r( $error_message );
} else {
//print_r( $response );
update_post_meta( $post_id, 'ahalogy_response', sanitize_text_field( time() ) );
}
}
}
}
// Verify we can use native JSON functions
function ahalogyMobileCheck() {
if (phpversion() < 5) {
add_action('admin_notices', array( &$this, 'ahalogyPHPWarning' ) );
return;
}
}
// Check for PHP version 5 or greater
function ahalogyPHPWarning() {
echo "
Sorry, the Ahalogy plugin requires PHP version 5.0 or greater.
";
}
//Authentication - check for API Key
function authenticateAPIKey() {
global $ahalogyWP_instance;
//Authentication - check for API Key
if (isset($_REQUEST['api_key'])) {
$apikey = $_REQUEST['api_key'];
if ($apikey != $ahalogyWP_instance->plugin_api_key) {
$response = array("status" => "error", "error" => "Ahalogy API key is not valid.");
$this->respond($response);
exit;
}
} else {
$response = array("status" => "error", "error" => "Ahalogy API key is required.");
$this->respond($response);
exit;
}
}
//redirect to JSON template if necessary
function jsonTemplateRedirect() {
global $ahalogyWP_instance;
//Check that is this is a single post
if(is_single()) {
if (isset($_REQUEST['mobilify_json'])) {
if ($_REQUEST['mobilify_json'] == 1) {
//Authenticate the api key
$this->authenticateAPIKey();
//Post is single and has the ahalogy_json parameter. Let's redirect it.
global $post;
$response = array(
'post' => new Mobilify_JSON_API_Post($post)
);
$this->respond($response);
exit;
}
}
}
//Check if it's the homepage and they are trying to hit the index API
if (is_front_page()) {
if (isset($_REQUEST['mobilify_json'])) {
if ($_REQUEST['mobilify_json'] == 1) {
//mobilify_json is true. Check the method
if (isset($_REQUEST['mobilify_index'])) {
if ($_REQUEST['mobilify_index'] == 1) {
//Authenticate the API Key
$this->authenticateAPIKey();
//mobilify_index is true. Show index json
// Arguments for WP_Query
$args =
array(
'posts_per_page' => -1,
'orderby' => 'modified'
);
//Check for modified_since date parameter
if (isset($_REQUEST['modified_since'])) {
$modifieddate = $_REQUEST['modified_since'];
if ($this->is_timestamp($modifieddate)) {
$moddatearray = getdate($modifieddate);
$args['date_query'] = array(
'column' => 'post_modified_gmt',
'after' =>
array(
'year' => $moddatearray['year'],
'month' => $moddatearray['mon'],
'day' => $moddatearray['mday'],
)
);
}
}
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$response = array();
while ( $the_query->have_posts() ) {
$the_query->the_post();
global $post;
$postoutput['id'] = $post->ID;
$postoutput['title'] = $post->post_title;
$postoutput['url'] = get_permalink($post->id);
$postoutput['modified'] = date($ahalogyWP_instance->date_format, strtotime($post->post_modified));
$response['posts'][] = $postoutput;
}
} else {
$response = array("status" => "no results");
}
$this->respond($response);
exit;
}
}
}
}
}
}
function get_json($data, $status = 'ok') {
// Include a status value with the response
if (is_array($data)) {
$data = array_merge(array('status' => $status), $data);
} else if (is_object($data)) {
$data = get_object_vars($data);
$data = array_merge(array('status' => $status), $data);
}
if (function_exists('json_encode')) {
// Use the built-in json_encode function if it's available
$json = json_encode($data);
} else {
// Use PEAR's Services_JSON encoder otherwise
if (!class_exists('Services_JSON')) {
require_once dirname(__FILE__) . "/library/JSON.php";
}
$json_service = new Services_JSON();
$json = $json_service->encode($data);
}
return $json;
}
// JSON Output
function output($result) {
$charset = get_option('blog_charset');
if (!headers_sent()) {
header('HTTP/1.1 200 OK', true);
header("Content-Type: application/json; charset=$charset", true);
}
echo $result;
}
function respond($result, $status = 'ok') {
$json = $this->get_json($result, $status);
// Output the result
$this->output($json);
exit;
}
//Validate our modified_date timestamp
function is_timestamp($timestamp) {
$check = (is_int($timestamp) OR is_float($timestamp))
? $timestamp
: (string) (int) $timestamp;
return ($check === $timestamp)
AND ( (int) $timestamp <= PHP_INT_MAX)
AND ( (int) $timestamp >= ~PHP_INT_MAX);
}
} //end class
endif;
$ahalogyWPMobile_instance = new ahalogyWPMobile;