plugin_args = get_option('srp_plugin_options');
// Including external widget values.
global $srp_default_widget_values;
// Double check if $args is an array.
if (!is_array($args)) {
$args = array();
}
// Setting up widget options to be available throughout the plugin.
$this->widget_args = array_merge($srp_default_widget_values, $args);
// Updating thumbnails sizes if these are not set on custom PHP call. Assign default value.
if (!isset($this->widget_args["srp_thumbnail_wdg_width"])) {
$this->widget_args["srp_thumbnail_wdg_width"] = $this->plugin_args["srp_thumbnail_width"];
}
if (!isset($this->widget_args["srp_thumbnail_wdg_height"])) {
$this->widget_args["srp_thumbnail_wdg_height"] = $this->plugin_args["srp_thumbnail_height"];
}
// Setting up post/page ID when on a single post/page.
if (is_single() || is_page()) {
// Including global $post object.
global $post;
// Assigning post ID.
$this->singleID = $post->ID;
}
}
// Class Deconstructor.
public function __deconstruct() {}
/*
| ---------------------------------------------
| STATIC METHODS
| ---------------------------------------------
*/
// This method handles all the actions for the plugin to be initialized.
static function install_plugin() {
// Loading text domain for translations.
load_plugin_textdomain(SRP_TRANSLATION_ID, false, dirname(plugin_basename(__FILE__)) . SRP_LANG_FOLDER);
// Importing global default options array.
global $srp_default_plugin_values;
// Creating WP Option with default values.
add_option('srp_plugin_options', $srp_default_plugin_values, '', 'no');
}
// This method handles all the actions for the plugin to be uninstalled.
static function uninstall_plugin() {
// Deleting main WP Option.
delete_option('srp_plugin_options');
}
/*
| ---------------------------------------------
| CLASS MAIN METHODS
| ---------------------------------------------
*/
// Method to retrieve first post image url.
private function getFirstImageUrl($thumb_width, $thumb_height, $post_title) {
// Including global WP Enviroment.
global $post, $posts;
//Getting images attached to the post.
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'order' => 'ASC',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
// Check for attachments.
if ($attachments) {
// Cycling through attachments.
foreach ($attachments as $attachment) {
// Retrieving image url.
$imgabs = base64_encode(get_attached_file($attachment->ID));
break;
}
//Return image tag using adaptive resize with PHPThumb.
return "widget_args["srp_thumbnail_rotation"] . "&file=" . $imgabs . "\" />";
} else {
// there are no attachment for the current post. Switch to default thumbnail image.
return $this->displayDefaultThumb($this->widget_args["srp_thumbnail_wdg_width"], $this->widget_args["srp_thumbnail_wdg_height"]);
}
}
// Method to display default thumbnail.
private function displayDefaultThumb($thumb_width, $thumb_height) {
// Check if a custom thumbnail url has been provided.
if ($this->plugin_args['srp_thumbnail_url'] != '') {
// Returning custom default thumbnail image.
return "
plugin_args['srp_thumbnail_url'] . "\" width=\"" . $thumb_width . "\" height=\"" . $thumb_height . "\" />";
} else {
// Returning default thumbnail image.
return "
";
}
}
// Main method to extract and elaborate post excerpt based on user choices.
private function extractExcerpt($post) {
// Loading default plugin values.
$excerpt_length = $this->plugin_args['srp_excerpt_length'];
$excerpt_length_mode = $this->plugin_args['srp_excerpt_length_mode'];
// Check if widget values are overriding default ones.
if ($excerpt_length != $this->widget_args['srp_wdg_excerpt_length'])
$excerpt_length = $this->widget_args['srp_wdg_excerpt_length'];
if ($excerpt_length_mode != $this->widget_args['srp_wdg_excerpt_length_mode'])
$excerpt_length_mode = $this->widget_args['srp_wdg_excerpt_length_mode'];
// Check for "cut mode".
switch($excerpt_length_mode) {
case 'words':
// Check if excerpt is available.
if ($post->post_excerpt) {
// Return normal excerpt using 'words cut'.
return $this->substrWords($this->srp_sanitize($post->post_excerpt), $excerpt_length);
} else {
// Normal excerpt not available. Retrieve text from post content using 'words cut'.
return $this->substrWords($this->srp_sanitize($post->post_content), $excerpt_length);
}
break;
case 'chars':
// Check if excerpt is available.
if ($post->post_excerpt) {
// Return normal excerpt using 'characters cut'.
return substr($this->srp_sanitize($post->post_excerpt), 0, $excerpt_length);
} else {
// Normal excerpt not available. Retrieve text from post content using 'characters cut'.
return substr($this->srp_sanitize($post->post_content), 0, $excerpt_length);
}
break;
case 'fullexcerpt':
// Check if excerpt is available.
if ($post->post_excerpt) {
// Return normal excerpt using 'characters cut'.
return $this->srp_sanitize($post->post_excerpt);
} else {
// Normal excerpt not available. Retrieve text from post content using 'characters cut'.
return substr($this->srp_sanitize($post->post_content), 0, $excerpt_length);
}
break;
}
}
// Main method to extract and elaborate post title based on user choices.
private function extractTitle($post) {
// Loading default plugin values.
$title_length = $this->plugin_args['srp_title_length'];
$title_length_mode = $this->plugin_args['srp_title_length_mode'];
// Check if widget values are overriding default ones.
if ($title_length != $this->widget_args['srp_wdg_title_length'])
$title_length = $this->widget_args['srp_wdg_title_length'];
if ($title_length_mode != $this->widget_args['srp_wdg_title_length_mode'])
$title_length_mode = $this->widget_args['srp_wdg_title_length_mode'];
// Check for "cut mode".
switch($title_length_mode) {
case 'words':
// Return normal title using 'words cut'.
return $this->substrWords($this->srp_sanitize($post->post_title), $title_length);
break;
case 'chars':
// Return normal title using 'characters cut'.
return substr($this->srp_sanitize($post->post_title), 0, $title_length);
break;
case 'fulltitle':
// Return normal title using 'characters cut'.
return $this->srp_sanitize($post->post_title);
break;
}
}
// Main method to retrieve posts.
private function getPosts() {
// Defining args array.
$args = array (
'post_type' => $this->widget_args["srp_post_type"],
'numberposts' => $this->widget_args["srp_number_post_option"],
'order' => $this->widget_args["srp_order_post_option"],
'post_status' => $this->widget_args["srp_post_status_option"]
);
// Check for custom pot types option.
if ($this->widget_args["srp_custom_post_type_option"] != '') {
// Filter result posts by category ID.
$args["post_type"] = $this->widget_args["srp_custom_post_type_option"];
}
// Check if category filter is applied.
if ($this->widget_args["srp_filter_cat_option"] != '-1') {
// Filter result posts by category ID.
$args["category"] = $this->widget_args["srp_filter_cat_option"];
}
// Check if post offset option is enabled.
if ($this->plugin_args["srp_post_offset"] == 'yes') {
// Filter result posts by category ID.
$args["exclude"] = $this->singleID;
}
// Check if global post offset option is enabled.
if ($this->widget_args["srp_post_global_offset_option"] != 0) {
// Filter result posts by category ID.
$args["offset"] = $this->widget_args["srp_post_global_offset_option"];
}
// Check if exclude posts option is applied.
if (!empty($this->widget_args["srp_exclude_option"])) {
// Filter result posts by category ID.
$args["exclude"] = $this->widget_args["srp_exclude_option"];
}
// Check if include posts option is applied.
if (!empty($this->widget_args["srp_include_option"])) {
// Filter result posts by category ID.
$args["include"] = $this->widget_args["srp_include_option"];
}
// Calling built-in Wordpress 'get_posts' function.
$result_posts = get_posts($args);
// Checking if result posts array is empty.
if (empty($result_posts)) {
// No recent posts available. Return empty array.
return $result_posts;
}
// Check if random posts option is checked.
if ($this->widget_args["srp_orderby_post_option"] != "") {
// Let's shuffle the result array.
shuffle($result_posts);
}
// Fix issues that let included IDs override the max number of post displayed.
$output_array = array_slice($result_posts, 0, $args["numberposts"]);
// Return result array.
return $output_array;
}
// Main method to display posts.
public function displayPosts($widget_call = NULL, $return_mode) {
// Declaring global $post variable.
global $post;
// Open Widget Container.
$srp_content = "
" . $this->srp_sanitize($this->plugin_args['srp_noposts_message']) . "
"; } else { // Recent posts are available. Cycle through result posts. foreach($this->getPosts() as $post) { // Prepare access to all post data. setup_postdata($post); // Single Post Container. $srp_content .= ""; } // EOF foreach cycle. // Reset $post data array. wp_reset_postdata(); } // EOF Empty posts check. $srp_content .= "