Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sidebar widget to display recent public (published) links #8

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions argo-link-roundups-widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/*
* argo Recent Posts
*/
class argo_link_roundups_widget extends WP_Widget {

function argo_link_roundups_widget() {
$widget_ops = array(
'classname' => 'argo-link-roundups',
'description' => 'Show your most recent link roundups in the sidebar', 'argo-links'
);
$this->WP_Widget( 'argo-link-roundups-widget', __('Argo Link Roundups Widget', 'argo-links'), $widget_ops);
}

function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );

echo $before_widget;

if ( $title )
echo $before_title . $title . $after_title;?>

<?php
$query_args = array (
'post__not_in' => get_option( 'sticky_posts' ),
'showposts' => $instance['num_posts'],
'exceprt' => $instance['show_excerpt'],
'post_type' => 'post',
'post_status' => 'publish'
);
$my_query = new WP_Query( $query_args );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) : $my_query->the_post();
$custom = get_post_custom($post->ID); ?>
<div class="post-lead clearfix">
<?php

// the date
$output .='<span>' . get_the_date("F d Y") . '</span>';

// the headline
$output .= '<h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>';

// the excerpt
$output .= '<p>' . get_the_excerpt() . '</p>';

echo $output;
?>

</div> <!-- /.post-lead -->
<?php
endwhile;
} else {
_e('<p class="error"><strong>You don\'t have any recent links or the argo links plugin is not active.</strong></p>', 'argo-links');
} // end recent links

if ( $instance['linkurl'] !='' ) { ?>
<p class="morelink"><a href="<?php echo $instance['linkurl']; ?>"><?php echo $instance['linktext']; ?></a></p>
<?php }
echo $after_widget;
}

function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['num_posts'] = strip_tags( $new_instance['num_posts'] );
$instance['linktext'] = $new_instance['linktext'];
$instance['linkurl'] = $new_instance['linkurl'];
return $instance;
}

function form( $instance ) {
$defaults = array(
'title' => 'Recent Link Roundups',
'num_posts' => 1,
'linktext' => '',
'linkurl' => ''
);

$instance = wp_parse_args( (array) $instance, $defaults );
?>

<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'argo-links'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
</p>

<p>
<label for="<?php echo $this->get_field_id( 'num_posts' ); ?>"><?php _e('Number of posts to show:', 'argo-links'); ?></label>
<input id="<?php echo $this->get_field_id( 'num_posts' ); ?>" name="<?php echo $this->get_field_name( 'num_posts' ); ?>" value="<?php echo $instance['num_posts']; ?>" style="width:90%;" />
</p>


<p><strong>More Link</strong><br /><small><?php _e('If you would like to add a more link at the bottom of the widget, add the link text and url here.', 'argo-links'); ?></small></p>
<p>
<label for="<?php echo $this->get_field_id('linktext'); ?>"><?php _e('Link text:', 'argo-links'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('linktext'); ?>" name="<?php echo $this->get_field_name('linktext'); ?>" type="text" value="<?php echo $instance['linktext']; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('linkurl'); ?>"><?php _e('URL:', 'argo-links'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('linkurl'); ?>" name="<?php echo $this->get_field_name('linkurl'); ?>" type="text" value="<?php echo $instance['linkurl']; ?>" />
</p>

<?php
}
}
30 changes: 15 additions & 15 deletions argo-link-roundups.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class ArgoLinkRoundups {

/* Initialize the plugin */
function init() {
public static function init() {
/*Register the custom post type of argolinks */
add_action('init', array(__CLASS__, 'register_post_type' ));

Expand All @@ -23,14 +23,14 @@ function init() {

/*Save our custom post fields! Very important!*/
add_action('save_post', array(__CLASS__, 'save_custom_fields'));

/*Make sure our custom post type gets pulled into the river*/
add_filter( 'pre_get_posts', array(__CLASS__,'my_get_posts') );
}

/*Pull the argolinkroundups into the rivers for is_home, is_tag, is_category, is_archive*/
/*Merge the post_type query var if there is already a custom post type being pulled in, otherwise do post & argolinkroundups*/
function my_get_posts( $query ) {
public static function my_get_posts( $query ) {
// bail out early if suppress filters is set to true
if ($query->get('suppress_filters')) return;

Expand All @@ -44,9 +44,9 @@ function my_get_posts( $query ) {
}
}
}

/*Register the Argo Links post type */
function register_post_type() {
public static function register_post_type() {
$argolinkroundups_options = array(
'labels' => array(
'name' => 'Link Roundups',
Expand All @@ -65,6 +65,7 @@ function register_post_type() {
'supports' => array( 'title', 'editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats'),
'public' => true,
'menu_position' => 7,
'menu_icon' => 'dashicons-list-view',
'taxonomies' => apply_filters('argolinkroundups_taxonomies', array('category','post_tag')),
'has_archive' => true,
);
Expand All @@ -73,14 +74,14 @@ function register_post_type() {
}
register_post_type('argolinkroundups', $argolinkroundups_options);
}

/*Tell Wordpress where to put our custom fields for our custom post type*/
function add_custom_post_fields() {
public static function add_custom_post_fields() {
add_meta_box("argo_link_roundups_roundup", "Recent Roundup Links", array(__CLASS__,"display_custom_fields"), "argolinkroundups", "normal", "high");

}
/*Show our custom post fields in the add/edit Argo Link Roundups admin pages*/
function display_custom_fields() {
public static function display_custom_fields() {
?>
<div id='argo-links-display-area'>
</div>
Expand All @@ -93,7 +94,7 @@ function display_custom_fields() {
}

/*Save the custom post field data. Very important!*/
function save_custom_fields($post_id) {
public static function save_custom_fields($post_id) {
if (isset($_POST["argo_link_url"])){
update_post_meta((isset($_POST['post_id']) ? $_POST['post_ID'] : $post_id), "argo_link_url", $_POST["argo_link_url"]);
}
Expand All @@ -102,20 +103,19 @@ function save_custom_fields($post_id) {
}
}
/*Add the Argo Link Roundup options sub menu*/
function add_argo_link_roundup_options_page() {
public static function add_argo_link_roundup_options_page() {
add_submenu_page( "edit.php?post_type=argolinkroundups", "Options", "Options", "edit_posts", "argo-link-roundups-options", array(__CLASS__, 'build_argo_link_roundups_options_page' ) );
//call register settings function
add_action( 'admin_init', array(__CLASS__,'register_mysettings') );
}


function register_mysettings() {
public static function register_mysettings() {
//register our settings
register_setting( 'argolinkroundups-settings-group', 'argo_link_roundups_custom_url' );
register_setting( 'argolinkroundups-settings-group', 'argo_link_roundups_custom_html' );
}

function build_argo_link_roundups_options_page() { ?>
public static function build_argo_link_roundups_options_page() { ?>
<?php
$default_html = <<<EOT
<p class='link-roundup'><a href='#!URL!#'>#!TITLE!#</a> &ndash; <span class='description'>#!DESCRIPTION!#</span> <em>#!SOURCE!#</em></p>
Expand Down Expand Up @@ -148,14 +148,14 @@ function build_argo_link_roundups_options_page() { ?>
</td>
</tr>
</table>

<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>

</form>
</div>
<?php }
<?php }
}
/* Initialize the plugin using it's init() function */
ArgoLinkRoundups::init();
Expand Down
116 changes: 116 additions & 0 deletions argo-links-widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/*
* argo Recent Posts
*/
class argo_links_widget extends WP_Widget {

function argo_links_widget() {
$widget_ops = array(
'classname' => 'argo-links',
'description' => 'Show your most recently saved links in a sidebar widget', 'argo-links'
);
$this->WP_Widget( 'argo-links-widget', __('Argo Links Widget', 'argo-links'), $widget_ops);
}

function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );

echo $before_widget;

if ( $title )
echo $before_title . $title . $after_title;?>

<?php
$query_args = array (
'post__not_in' => get_option( 'sticky_posts' ),
'showposts' => $instance['num_posts'],
'post_type' => 'argolinks',
'post_status' => 'publish'
);
$my_query = new WP_Query( $query_args );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) : $my_query->the_post();
$custom = get_post_custom($post->ID); ?>
<?php if (get_post_type($post) === 'argolinkroundups') continue; ?>
<div class="post-lead clearfix">
<h5><?php echo ( isset( $custom["argo_link_url"][0] ) ) ? '<a href="' . $custom["argo_link_url"][0] . '">' . get_the_title() . '</a>' : get_the_title(); ?></h5>

<?php
if ( isset( $custom["argo_link_description"][0] ) ) {
echo '<p class="description">';
echo ( function_exists( 'largo_trim_sentences' ) ) ? largo_trim_sentences($custom["argo_link_description"][0], $instance['num_sentences']) : $custom["argo_link_description"][0];
echo '</p>';
}
if ( isset($custom["argo_link_source"][0] ) ) {
echo '<p class="source">' . __('Source: ', 'argo-links') . '<span>';
echo ( isset( $custom["argo_link_url"][0] ) ) ? '<a href="' . $custom["argo_link_url"][0] . '">' . $custom["argo_link_source"][0] . '</a>' : $custom["argo_link_source"][0];
echo '</span></p>';
}
?>

</div> <!-- /.post-lead -->
<?php
endwhile;
} else {
_e('<p class="error"><strong>You don\'t have any recent links or the argo links plugin is not active.</strong></p>', 'argo-links');
} // end recent links

if ( $instance['linkurl'] !='' ) { ?>
<p class="morelink"><a href="<?php echo $instance['linkurl']; ?>"><?php echo $instance['linktext']; ?></a></p>
<?php }
echo $after_widget;
}

function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['num_posts'] = strip_tags( $new_instance['num_posts'] );
$instance['num_sentences'] = strip_tags( $new_instance['num_sentences'] );
$instance['linktext'] = $new_instance['linktext'];
$instance['linkurl'] = $new_instance['linkurl'];
return $instance;
}

function form( $instance ) {
$defaults = array(
'title' => 'Recent Links',
'num_posts' => 5,
'num_sentences' => 2,
'linktext' => '',
'linkurl' => ''
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>

<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'argo-links'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
</p>

<p>
<label for="<?php echo $this->get_field_id( 'num_posts' ); ?>"><?php _e('Number of posts to show:', 'argo-links'); ?></label>
<input id="<?php echo $this->get_field_id( 'num_posts' ); ?>" name="<?php echo $this->get_field_name( 'num_posts' ); ?>" value="<?php echo $instance['num_posts']; ?>" style="width:90%;" />
</p>

<?php if ( function_exists( 'largo_trim_sentences' ) ) : ?>
<p>
<label for="<?php echo $this->get_field_id( 'num_sentences' ); ?>"><?php _e('Excerpt Length (# of Sentences):', 'argo-links'); ?></label>
<input id="<?php echo $this->get_field_id( 'num_sentences' ); ?>" name="<?php echo $this->get_field_name( 'num_sentences' ); ?>" value="<?php echo $instance['num_sentences']; ?>" style="width:90%;" />
</p>
<?php endif; ?>

<p><strong>More Link</strong><br /><small><?php _e('If you would like to add a more link at the bottom of the widget, add the link text and url here.', 'argo-links'); ?></small></p>
<p>
<label for="<?php echo $this->get_field_id('linktext'); ?>"><?php _e('Link text:', 'argo-links'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('linktext'); ?>" name="<?php echo $this->get_field_name('linktext'); ?>" type="text" value="<?php echo $instance['linktext']; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('linkurl'); ?>"><?php _e('URL:', 'argo-links'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('linkurl'); ?>" name="<?php echo $this->get_field_name('linkurl'); ?>" type="text" value="<?php echo $instance['linkurl']; ?>" />
</p>

<?php
}
}
Loading