/** * Display random posts with shortcode * * @since 0.0.1 */ function arpw_shortcode($atts, $content) { $args = shortcode_atts(arpw_get_default_args(), $atts); return arpw_get_random_posts($args); }
/** * Displays the widget control options in the Widgets admin screen. * * @since 0.0.1 */ function form($instance) { // Merge the user-selected arguments with the defaults. $instance = wp_parse_args((array) $instance, arpw_get_default_args()); // Extract the array to allow easy use of variables. extract($instance); // Loads the widget form. include ARPW_INC . 'form.php'; }
/** * Generates the random posts markup. * * @since 0.0.1 * @param array $args * @return string|array The HTML for the random posts. */ function arpw_get_random_posts($args = array()) { // Set up a default, empty $html variable. $html = ''; // Get the default arguments. $defaults = arpw_get_default_args(); // Merge the input arguments and the defaults. $args = wp_parse_args($args, $defaults); // Extract the array to allow easy use of variables. extract($args); // Allow devs to hook in stuff before the loop. do_action('arpw_before_loop', $args); // Get the posts query. $posts = arpw_get_posts($args); if ($posts->have_posts()) { $html = '<div class="arpw-random-' . sanitize_html_class($args['post_type']) . ' ' . sanitize_html_class($args['css_class']) . '">'; $html .= '<ul class="arpw-ul">'; while ($posts->have_posts()) { $posts->the_post(); $html .= '<li class="arpw-li arpw-clearfix">'; if ($args['thumbnail']) { // Check if post has post thumbnail. if (has_post_thumbnail()) { // Custom thumbnail sizes. $thumb_id = get_post_thumbnail_id(); // Get the featured image id. $img_url = wp_get_attachment_url($thumb_id); // Get img URL. $image = arpw_resize($img_url, $args['thumbnail_width'], $args['thumbnail_height'], true); $html .= '<a href="' . esc_url(get_permalink()) . '" rel="bookmark">'; if ($args['thumbnail_custom']) { $html .= '<img class="arpw-thumbnail align' . esc_attr($args['thumbnail_align']) . '" src="' . esc_url($image) . '" alt="' . esc_attr(get_the_title()) . '">'; } else { $html .= get_the_post_thumbnail(get_the_ID(), $args['thumbnail_size'], array('alt' => esc_attr(get_the_title()), 'class' => 'arpw-thumbnail align' . esc_attr($args['thumbnail_align']))); } $html .= '</a>'; // If no post thumbnail found, check if Get The Image plugin exist and display the image. } elseif (function_exists('get_the_image')) { if ($args['thumbnail_custom']) { $html .= get_the_image(array('width' => (int) $args['thumbnail_width'], 'height' => (int) $args['thumbnail_height'], 'image_class' => 'arpw-thumbnail align' . esc_attr($args['thumbnail_align']), 'image_scan' => true, 'echo' => false, 'link_to_post' => true)); } else { $html .= get_the_image(array('size' => $args['thumbnail_size'], 'image_class' => 'arpw-thumbnail align' . esc_attr($args['thumbnail_align']), 'image_scan' => true, 'echo' => false, 'link_to_post' => true)); } // Display nothing. } else { $html .= null; } } $html .= '<a class="arpw-title" href="' . esc_url(get_permalink()) . '" title="' . sprintf(esc_attr__('Permalink to %s', 'arpw'), the_title_attribute('echo=0')) . '" rel="bookmark">' . esc_attr(get_the_title()) . '</a>'; if ($args['date']) { $date = get_the_date(); if ($args['date_relative']) { $date = sprintf(__('%s ago', 'arpw'), human_time_diff(get_the_date('U'), current_time('timestamp'))); } $html .= '<time class="arpw-time published" datetime="' . esc_html(get_the_date('c')) . '">' . esc_html($date) . '</time>'; } elseif ($args['date_modified']) { // if both date functions are provided, we use date to be backwards compatible $date = get_the_modified_date(); if ($args['date_relative']) { $date = sprintf(__('%s ago', 'rpwe'), human_time_diff(get_the_modified_date('U'), current_time('timestamp'))); } $html .= '<time class="arpw-time modfied" datetime="' . esc_html(get_the_modified_date('c')) . '">' . esc_html($date) . '</time>'; } if ($args['excerpt']) { $html .= '<div class="arpw-summary">' . wp_trim_words(apply_filters('arpw_excerpt', get_the_excerpt()), $args['excerpt_length'], ' …') . '</div>'; } $html .= '</li>'; } $html .= '</ul>'; $html .= '</div><!-- Generated by https://wordpress.org/plugins/advanced-random-posts-widget/ -->'; } // Restore original Post Data. wp_reset_postdata(); // Allow devs to hook in stuff after the loop. do_action('arpw_after_loop', $args); // Return the related posts markup. return $args['before'] . $html . $args['after']; }