base_url = get_option('siteurl').'/'.Str_Replace("\\", '/', SubStr(RealPath(DirName(__FILE__)), Strlen(ABSPATH)));
// Get ready to translate
$this->Load_TextDomain();
// Hooks
Add_Action('admin_menu', Array($this, 'prepare_settings_field'));
Add_Action('admin_menu', Array($this, 'add_meta_box'));
Add_Action('save_post', Array($this, 'save_meta_box_inputs'));
Add_Filter('the_content', Array($this, 'append_posts_to_page'));
// Shortcodes
Add_Shortcode('associated_posts', Array($this, 'show_associated_posts'));
// Settings Key
$this->settings_key = 'associated_posts';
}
Function Load_TextDomain(){
$this->text_domain = get_class($this);
load_textdomain ($this->text_domain, DirName(__FILE__).'/language/'.get_locale().'.mo');
}
Function t ($text, $context = ''){
// Translates the string $text with context $context
If ($context == '')
return __($text, $this->text_domain);
Else
return _x($text, $context, $this->text_domain);
}
Function Save_Setting($key, $value){
$arr_setting = get_option($this->settings_key);
If ($key == Null){
$arr_setting = (Array) $value;
}
Else {
If ($value == Null)
Unset ($arr_setting[$key]);
Else
$arr_setting[$key] = $value;
}
update_option($this->settings_key, $arr_setting);
}
Function Load_Setting($key = Null){
$arr_setting = (Array) get_option($this->settings_key);
If ($key == Null)
return $arr_setting;
ElseIf (IsSet ($arr_setting[$key]))
return $arr_setting[$key];
Else
return False;
}
Function Field_Name ($key){
return $this->settings_key.'['.$key.']';
}
Function prepare_settings_field (){
// Check if there is a post request
If ( $_REQUEST['option_page'] == 'reading' && $_REQUEST['action'] == 'update' )
$this->Save_Setting(Null, (Array) $_REQUEST[$this->settings_key]);
// Add Settings Field
add_settings_field(get_class($this), $this->t('Associated posts'), Array($this, 'add_settings_field'), 'reading');
}
Function add_settings_field(){
?>
t('Theme Settings') ?>
Load_Setting('auto_append'), 'yes') ?>/>
t('My Theme does not support associated posts. Append the posts to the end of the pages content.'); ?>
t('ShortCode Settings') ?>
t('These Settings only affect the [associated_posts] ShortCode. If your theme supports associated posts you should do the settings in your theme settings.') ?>
Load_Setting('show_excerpt'), 'yes') ?>/>
t('Show an excerpt of the associated posts.'); ?>
Load_Setting('show_thumbnail'), 'yes') ?>/>
t('Show a post thumbnail of the associated posts.'); ?>
t('Associate posts with this page'),
Array($this, 'print_meta_box'),
'page',
'advanced',
'high' );
}
Function print_meta_box(){
Global $post;
$post_id = $post->ID;
$association_settings = get_post_meta($post_id, '_association_settings', True);
?>
t('Please choose a category which contains posts you want to associate with this page.') ?>
t('Category:') ?>
t('How much posts should be shown?') ?>
(t('Leave blank to show all.') ?>)
ID, $arr_post_done) !== False) return False;
// Perceive what we have done
$arr_post_done[] = $post->ID;
}
// Here we will buffer the output
$content = '';
// Get association settings
$association_settings = get_post_meta($post->ID, '_association_settings', True);
// Read how many posts should be shown
If ($association_settings['post_limit'] == '')
$limit = -1;
Else
$limit = IntVal ($association_settings['post_limit']);
// Read associated posts and bulild html
If ($associated_posts = $this->get_associated_posts($post->ID, $limit)){
// Print the associated posts (append to $content)
$content .= '
';
}
return $content;
}
Function get_associated_posts ($post_id = Null, $limit = Null){
// If there is no post_id we try to ready it
If ($post_id == Null) $post_id = get_the_id();
// If there is even no post_id we bail out
If (!$post_id) return False;
// read the associated category
$association_settings = get_post_meta($post_id, '_association_settings', True);
// Check if there are settings
If (!Is_Array($association_settings)) return False;
// check if there is a category
If ($association_settings['associated_cat_id'] == '') return False;
// Read how many posts should be shown
If ($limit == Null)
$limit = IntVal($association_settings['post_limit']);
ElseIf ($limit == 0)
$limit = -1;
Else
$limit = IntVal ($limit);
// Get the posts
$associated_posts = get_posts (Array( 'cat' => $association_settings['associated_cat_id'],
'numberposts' => $limit ));
If (!Empty($associated_posts))
return $associated_posts;
Else
return False;
}
Function get_associated_cat_id ($post_id = Null){
If ($post_id == Null) $post_id = get_the_id();
If (!$post_id) return False;
$association_settings = get_post_meta($post_id, '_association_settings', True);
If (!Is_Array($association_settings)) return False;
return $association_settings['associated_cat_id'];
}
Function append_posts_to_page($content){
If ($this->Load_Setting('auto_append'))
return $content . '[associated_posts]';
Else
return $content;
}
Function get_post_thumbnail($post_id = Null, $size = 'thumbnail'){
If ($post_id == Null){
Global $post;
$post_id = $post->ID;
}
// Try to find a thumbnail of the post
If (Function_Exists('get_post_thumbnail_id') && $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), $size)){
// If the user has set a post thumb we use it
return $thumb;
}
Else {
// If there is now thumb we use a random image from this post
$arr_attachment = (Array) get_posts (Array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'orderby' => 'ASC' ));
// Check if there are attachments
If (Empty($arr_attachment)) return False;
Shuffle ($arr_attachment);
$thumb = wp_get_attachment_image_src($arr_attachment[0]->ID, $size);
/* Return Value: An array containing:
$image[0] => url
$image[1] => width
$image[2] => height
*/
}
// If we found a thumb we use it
If (!Empty($thumb))
return $thumb;
Else
return False;
}
Function get_the_excerpt($post_id = Null){
If ($post_id == Null) $post_id = get_the_ID();
// Get the post
$post = &get_post ($post_id);
// check if password is required
If ( post_password_required($post) ){
$excerpt = __('There is no excerpt because this is a protected post.'); // translation in the core text domain
return $excerpt;
}
If ($post->post_excerpt != ''){
// get the users excerpt
$excerpt = $post->post_excerpt;
}
Else {
// Create the excerpt
$excerpt = $post->post_content;
$excerpt = strip_shortcodes( $excerpt );
$excerpt = apply_filters('the_content', $excerpt);
$excerpt = str_replace(']]>', ']]>', $excerpt);
$excerpt = strip_tags($excerpt);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', '[...]');
$words = explode(' ', $excerpt, $excerpt_length + 1);
If ( Count($words) > $excerpt_length ){
Array_Pop($words);
Array_Push($words, $excerpt_more);
$excerpt = Implode(' ', $words);
}
}
// Run filters - there is a bug if you try to run an empty excerpt through the filter
If ($excerpt != '') $excerpt = apply_filters('get_the_excerpt', $excerpt);
return $excerpt;
}
} /* End of Class */
New wp_plugin_associate_posts_and_pages();
} /* End of If-Class-Exists-Condition */
/* End of File */