arlima_plugin = $arlima_plugin;
$this->factory = new Arlima_ListFactory();
}
/**
* Setup all ajax functions
*/
public function initActions()
{
add_action('wp_ajax_arlima_query_posts', array($this, 'queryPosts'));
add_action('wp_ajax_arlima_get_scissors', array($this, 'getScissors'));
add_action('wp_ajax_arlima_get_attached_images', array($this, 'getAttachedImages'));
add_action('wp_ajax_arlima_get_post', array($this, 'getPost'));
add_action('wp_ajax_arlima_add_list_widget', array($this, 'addListWidget'));
add_action('wp_ajax_arlima_check_for_later_version', array($this, 'checkForLaterVersion'));
add_action('wp_ajax_arlima_save_list', array($this, 'saveList'));
add_action('wp_ajax_arlima_prepend_article', array($this, 'prependArticle'));
add_action('wp_ajax_arlima_save_list_setup', array($this, 'saveListSetup'));
add_action('wp_ajax_arlima_get_list_setup', array($this, 'getListSetup'));
add_action('wp_ajax_arlima_upload', array($this, 'upload'));
add_action('wp_ajax_arlima_print_custom_templates', array($this, 'printCustomTemplates'));
add_action('wp_ajax_arlima_duplicate_image', array($this, 'duplicateImage'));
add_action('wp_ajax_arlima_import_arlima_list', array($this, 'importList'));
add_action('wp_ajax_arlima_remove_image_versions', array($this, 'removeImageVersions'));
// The following action is not possible to hook into wtf???
// add_action('wp_ajax_image-editor', array($this, 'removeImageVersions'));
if ( $this->isSavingEditedImage() ) {
add_action('init', array($this, 'removeImageVersions'));
}
}
/**
* Tells whether or not this request is made to save an edited image.
* This is a workaround for add_action('wp_ajax_image-editor', ...)
* @return bool
*/
private function isSavingEditedImage()
{
return isset($_POST['action']) &&
isset($_POST['postid']) &&
isset($_POST['do']) &&
isset($_POST['context']) &&
$_POST['action'] == 'image-editor' &&
$_POST['do'] == 'save' &&
$_POST['context'] == 'edit-attachment' &&
basename($_SERVER['PHP_SELF']) == 'admin-ajax.php';
}
/**
* Removes all arlima image versions (nothing will happen if WP version < 3.5)
*/
public function removeImageVersions()
{
// Arlima admin request
if( !empty($_POST['attachment']) ) {
$this->initAjaxRequest();
Arlima_ImageVersionManager::removeVersions($_POST['attachment']);
die( json_encode(array('success'=>true)));
}
// image editor in wp-admin
elseif( !empty($_POST['postid']) && is_user_logged_in() ) {
Arlima_ImageVersionManager::removeVersions($_POST['postid']);
}
else {
die( json_encode(array('error'=>'No attachment given')));
}
}
/**
* Import an external arlima list or RSS feed
*/
function importList()
{
$this->initAjaxRequest(false);
$import_manager = new Arlima_ImportManager($this->arlima_plugin);
try {
$list = $import_manager->importList($_POST['url']);
Arlima_ImportManager::displayImportedList($_POST['url'], $list['title']);
} catch (Exception $e) {
echo '
' . $e->getMessage() . '
';
}
die;
}
/**
* Make copy of an wordpress attachment
*/
function duplicateImage()
{
$this->initAjaxRequest();
if ( !function_exists('wp_generate_attachment_metadata') ) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
$attachment_id = intval($_POST['attachid']);
if ( $attachment_id ) {
$url = wp_get_attachment_url($attachment_id);
if( !$url )
die;
$tmp = download_url($url);
$file_array = array(
'name' => basename($url),
'tmp_name' => $tmp
);
// Check for download errors
if ( is_wp_error($tmp) ) {
@unlink($file_array['tmp_name']);
return $tmp;
}
$attach_id = media_handle_sideload($file_array, 0);
// Check for handle sideload errors.
if ( is_wp_error($attach_id) ) {
@unlink($file_array['tmp_name']);
return $attach_id;
}
echo json_encode(
array(
'attach_id' => $attach_id,
'html' => wp_get_attachment_image($attach_id, 'large'),
'error' => false
)
);
}
die();
}
/**
* Get arlima templates
*/
function printCustomTemplates()
{
$templates = array(
array(
'name' => 'Full featured teaser',
'title' => 'Lorem te ipusm dolor sit amet',
'text' => 'Lorem te ipsum dolor sit amet anno del torro.
',
'url' => 'http://google.com/',
'title_fontsize' => 20,
'options' => array(
'streamer' => '1',
'streamer_type' => 'text',
'streamer_color' => '3399ff', // Important, no #-sign
'streamer_image' => '',
'streamer_content' => 'Wild thing!',
'pre_title' => 'Demo:'
)
),
array(
'name' => 'Empty teaser',
'title' => '',
'text' => '',
'url' => ''
)
);
// Make it possible for theme to override templates
$templates = apply_filters('arlima_teaser_templates', $templates);
ob_start();
?>
$html, "articles" => $templates);
echo json_encode($data);
die();
}
/**
* Check logged in and correct nonce
*/
private function initAjaxRequest($send_json=true)
{
if( $send_json ) {
header('Content-Type: application/json');
}
if( !check_ajax_referer('arlima-nonce') ) {
die(json_encode(array('error' => 'incorrect nonce')));
} elseif( !is_user_logged_in() ) {
die(json_encode(array('error' => 'not logged in')));
}
}
/**
* Upload an image
*/
function upload()
{
$this->initAjaxRequest();
if ( !function_exists('wp_generate_attachment_metadata') ) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
$post_id = intval($_POST['postid']);
// image file from user's desktop
if ( !empty($_FILES) ) {
foreach ($_FILES as $file => $array) {
if ( $_FILES[$file]['error'] !== UPLOAD_ERR_OK ) {
die(json_encode(array('error' => 'upload error: ' . $_FILES[$file]['error'])));
}
if ( is_numeric($post_id) ) {
$attach_id = media_handle_upload($file, $post_id);
} else {
$upload = wp_handle_upload($_FILES[$file], array('test_form' => false));
if ( !isset($upload['error']) && isset($upload['file']) ) {
$wp_filetype = wp_check_filetype(basename($upload['file']), null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload['file'])),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $upload['file']);
$attach_data = wp_generate_attachment_metadata($attach_id, $upload['file']);
wp_update_attachment_metadata($attach_id, $attach_data);
}
}
}
} // image from the web
elseif ( !empty ($_POST['imgurl']) ) {
if ( is_numeric($post_id) ) {
media_sideload_image(urldecode($_POST['imgurl']), $post_id, '');
$attachments = get_posts(
array(
'post_type' => 'attachment',
'number_posts' => 1,
'post_status' => null,
'post_parent' => $post_id,
'orderby' => 'post_date',
'order' => 'DESC',
)
);
$attach_id = $attachments[0]->ID;
} else {
$url = $_POST[ 'imgurl' ];
$tmp = download_url( $url );
$file_array = array(
'name' => basename( $url ),
'tmp_name' => $tmp
);
/* @var WP_Error|string $tmp */
// Check for download errors
if ( is_wp_error( $tmp ) ) {
@unlink( $file_array[ 'tmp_name' ] );
die( json_encode( array( 'error' => $tmp->get_error_messages() ) ) );
}
$attach_id = media_handle_sideload( $file_array, 0 );
// Check for handle sideload errors.
if ( is_wp_error( $attach_id ) ) {
@unlink( $file_array['tmp_name'] );
die( json_encode( array( 'error' => $attach_id->error_message() ) ) );
}
}
} else {
die(json_encode(array('error' => 'no file')));
}
if ( empty($attach_id) ) {
die(json_encode(array('error' => 'no attach_id')));
}
echo json_encode(
array('attach_id' => $attach_id, 'html' => wp_get_attachment_image($attach_id, 'default'), 'error' => false)
);
die();
}
/**
* Get the list setup for currently logged in user
*/
function getListSetup()
{
$this->initAjaxRequest();
global $current_user;
get_currentuserinfo();
$setup = get_user_meta($current_user->ID, 'arlima-list-setup', true);
if ( !$setup ) {
$setup = array();
}
die(json_encode($setup));
}
/**
* Saves the user setup (lists to load on startup and their position and size)
*/
function saveListSetup()
{
$this->initAjaxRequest();
global $current_user;
get_currentuserinfo();
$lists = isset($_POST['lists']) ? $_POST['lists'] : null;
if ( $lists ) {
update_user_meta($current_user->ID, 'arlima-list-setup', $lists);
} else {
delete_user_meta($current_user->ID, 'arlima-list-setup');
}
die(json_encode(array()));
}
/**
* Prepend an article to the top of a list
*/
function prependArticle()
{
$this->initAjaxRequest();
global $current_user, $post;
get_currentuserinfo();
$list_id = isset($_POST['alid']) ? intval($_POST['alid']) : false;
$post_id = isset($_POST['postid']) ? intval($_POST['postid']) : false;
if ( $list_id && $post_id ) {
$post = get_post($post_id);
setup_postdata($post);
$GLOBALS['post'] = $post; // Soemhting is removing post from global, even though we call setup_postdata
if ( function_exists('vk_get_preamble') ) {
$text = vk_get_preamble();
} else {
// weird, "get_the_excerpt" should return the manual excerpt but does not seem to do this in the admin context
$text = !empty($post->post_excerpt) ? $post->post_excerpt : get_the_excerpt();
}
if ( stristr($text, '') === false ) {
$text = '
' . $text . '
';
}
$article = array(
'title' => $post->post_title,
'text' => $text,
'url' => get_permalink($post->ID),
'post_id' => $post->ID,
'title_fontsize' => 24
);
if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) {
$attach_id = get_post_thumbnail_id($post->ID, 'large');
$article['image'] = wp_get_attachment_url($attach_id);
$article['image_options'] = array(
'html' => get_the_post_thumbnail(
$post->ID,
'large',
array('class' => 'aligncenter')
),
'url' => wp_get_attachment_url($attach_id),
'attach_id' => $attach_id,
'size' => 'full',
'alignment' => 'aligncenter'
);
}
$list = $this->factory->loadList($list_id, false, true);
$articles = $list->getArticles();
array_unshift($articles, $article);
$this->saveAndOutputNewListVersion($list, $articles);
}
die(json_encode(array()));
}
/**
* Save a new version of a list
*/
function saveList()
{
$this->initAjaxRequest();
$list_id = isset($_POST['alid']) ? intval($_POST['alid']) : false;
if ( $list_id ) {
$articles = !empty($_POST['articles']) ? $_POST['articles'] : array();
$this->saveAndOutputNewListVersion($list_id, $articles, isset($_POST['preview']));
}
die;
}
/**
* @param Arlima_List|int $list_id
* @param $articles
* @param bool $preview
*/
private function saveAndOutputNewListVersion($list_id, $articles, $preview = false)
{
if ( $list_id instanceof Arlima_List ) {
$list = $list_id;
} else {
$list = $this->factory->loadList($list_id);
}
$this->factory->saveNewListVersion($list, $articles, get_current_user_id(), $preview);
// Reload list to get latest version
$list = $this->factory->loadList($list_id, false, true);
echo json_encode(
array(
'version' => $list->getVersion(),
'versioninfo' => $list->getVersionInfo(),
'versions' => $list->getVersions()
)
);
// custom hook that other plugins can add actions to.
do_action('arlima_save_list', $list);
}
/**
* Checks if there is a later version of the list that's about to be saved
*/
function checkForLaterVersion()
{
$this->initAjaxRequest();
$list_id = isset($_POST['alid']) ? (int)$_POST['alid'] : false;
$version = isset($_POST['version']) ? (int)$_POST['version'] : false;
if ( $list_id && $version ) {
$list = $this->factory->loadList($list_id);
if ( $list->getVersionAttribute('id') > $version ) {
echo json_encode(
array(
'version' => $list->getVersion(),
'versioninfo' => $list->getVersionInfo()
)
);
die;
}
}
echo json_encode(array('version' => false));
die;
}
/**
* Fetches an arlima list and outputs it in widget form
*/
function addListWidget()
{
$this->initAjaxRequest();
$list_id = isset($_POST['alid']) ? trim($_POST['alid']) : null;
$version = isset($_POST['version']) && is_numeric($_POST['version']) ? (int)$_POST['version'] : false;
if ( is_numeric($list_id) ) {
$list = $this->factory->loadList($list_id, $version, true);
$this->loadListWidgets($list);
} elseif ( $list_id ) {
// Probably url referring to an imported list
try {
$import_manager = new Arlima_ImportManager($this->arlima_plugin);
$list = $import_manager->loadList($list_id);
$this->loadImportedListWidget($list);
} catch (Exception $e) {
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(array('error' => $e->getMessage()));
}
}
die();
}
/**
* Outputs a list in json format
* @param Arlima_List $list
*/
private function loadListWidgets($list)
{
ob_start();
$connector = new Arlima_ListConnector($list);
$preview_page = current($connector->loadRelatedPages());
$preview_url = '';
$preview_page_width = '';
// Get article width from a related page
if( $preview_page ) {
$preview_url = get_permalink($preview_page->ID);
$relation = $connector->getRelationData($preview_page->ID);
$preview_page_width = $relation['attr']['width'];
}
// Get article width form a widget where the list is used
elseif( $widget = current($connector->loadRelatedWidgets()) ) {
$preview_page_width = $widget['width'];
}
?>
$html,
'articles' => $list->getArticles(),
'version' => $list->getVersion(),
'versioninfo' => $list->getVersionInfo(),
'versions' => $list->getVersions(),
'title_element' => $list->getTitleElement(),
'is_imported' => 0,
'exists' => $list->exists()
);
echo json_encode($data);
}
/**
* Outputs an imported list
* @param Arlima_List $list
*/
private function loadImportedListWidget($list)
{
$version_info = sprintf(
__('Last modified %s a go', 'arlima'),
human_time_diff($list->getVersionAttribute('created'))
);
ob_start();
?>
$html,
'articles' => $list->getArticles(),
'version' => 0,
'versioninfo' => $version_info,
'versions' => array(),
'is_imported' => 1,
'exists' => $list->exists()
);
echo json_encode($data);
}
/**
* Get a wordpress post in json format
*/
function getPost()
{
$this->initAjaxRequest();
$post_id = intval($_POST['postid']);
$post = get_post($post_id);
if ( is_object($post) && $post->post_type == 'post' && $post->post_status != 'deleted' && $post->post_status != 'trash' ) {
$post->url = get_permalink($post->ID);
$post->publish_date = strtotime($post->post_date_gmt);
echo json_encode((array)$post);
die;
}
die(json_encode(array()));
}
/**
* Get post attachments
*/
function getAttachedImages()
{
$this->initAjaxRequest();
$post_id = intval($_POST['postid']);
$args = array(
'order' => 'DESC',
'post_parent' => $post_id,
'post_status' => null,
'post_type' => 'attachment',
'post_mime_type' => 'image'
);
$images = array();
$attachments = get_children($args);
if ( $attachments ) {
foreach ($attachments as $attachment) {
$images[] = array(
'attach_id' => $attachment->ID,
'thumb' => wp_get_attachment_image($attachment->ID, 'thumbnail'),
'large' => wp_get_attachment_image($attachment->ID, 'large')
);
}
}
echo json_encode($images);
die();
}
function getScissors()
{
$this->initAjaxRequest();
$attachment_id = $_POST['attachment_id'];
if ( Arlima_Plugin::isScissorsInstalled() ) {
$scissors_output = '';
$thumb = get_post($attachment_id);
$scissors_output = scissors_media_meta($scissors_output, $thumb);
if ( !empty($scissors_output) ) {
echo $scissors_output;
}
}
die(json_encode(array()));
}
/**
* Search for posts
*/
function queryPosts()
{
$this->initAjaxRequest();
$catid = !empty($_POST['catid']) ? $_POST['catid'] : false;
$search = !empty($_POST['search']) ? $_POST['search'] : false;
$author = !empty($_POST['author']) ? $_POST['author'] : false;
$offset = !empty($_POST['offset']) && is_numeric($_POST['offset']) ? (int)$_POST['offset'] : 0;
if ( $catid ) {
$args['cat'] = $catid;
}
if ( $author ) {
$args['author'] = $author;
}
$args['s'] = '';
if ( $search ) {
if ( is_numeric($search) ) {
$args['p'] = $search;
} else {
$args['s'] = $search;
}
}
$args['numberposts'] = 10;
if ( $offset ) {
$args['offset'] = $offset;
}
$args['post_status'] = array('publish', 'future');
$args['post_type'] = array('post', 'usernews');
// Possibly modified by other plugins or the theme (take a look at readme.txt for more info)
$args = Arlima_PostSearchModifier::filterWPQuery($args, $_POST);
$this->iteratePosts(query_posts($args), $offset);
die();
}
/**
* @param array $posts
* @param int $offset
*/
private function iteratePosts($posts, $offset = 0)
{
$articles = array();
ob_start();
?>
| |
Id |
|
|
|
|
0 ) { ?> «
|
|
= 10 ) { ?> »
|
post_excerpt) ? $post->post_excerpt : get_the_excerpt();
}
if ( stristr($text, '') === false ) {
$text = '
' . $text . '
';
}
$url = get_permalink($post->ID);
$article = array(
'post_id' => $post->ID,
'title' => $post->post_title,
'text' => $text,
'url' => $url,
'title_fontsize' => 24,
'content' => apply_filters('the_content', $post->post_content),
'publish_date' => strtotime($post->post_date_gmt)
);
if ( function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID) ) {
$attach_id = get_post_thumbnail_id($post->ID);
$article['image'] = wp_get_attachment_url($attach_id);
$article['image_options'] = array(
'html' => get_the_post_thumbnail($post->ID, 'large'),
'url' => wp_get_attachment_url($attach_id),
'attach_id' => $attach_id,
'size' => 'full',
'alignment' => 'aligncenter',
'connected' => true
);
}
$articles[] = $article;
?>
|
|
ID; ?> |
post_title,
'',
'',
$post->ID
); ?>post_status == 'future' ) {
echo ' (' . __(
'unpublished',
'arlima'
) . ') ';
} ?> |
post_author); ?> |
post_date)); ?> |
$html, "articles" => $articles);
echo json_encode($data);
}
}