Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeelia/bb3ea07a3b1958a8e8e5e0a9f850806b to your computer and use it in GitHub Desktop.
Save felipeelia/bb3ea07a3b1958a8e8e5e0a9f850806b to your computer and use it in GitHub Desktop.
<?php
/**
* The public-facing functionality of the plugin.
*
* @link https://dualcon.ddns.net/
* @since 1.0.0
*
* @package My_Wp_Tools
* @subpackage My_Wp_Tools/public
*/
/**
* The public-facing functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the public-facing stylesheet and JavaScript.
*
* @package My_Wp_Tools
* @subpackage My_Wp_Tools/public
* @author Dualcon <dualconcompany@gmail.com>
*/
class My_Wp_Tools_Public {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of the plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
}
/**
* Register the stylesheets for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_styles() {
/**
* Load Bootstrap.
*/
wp_enqueue_style( 'bootstrap-css', plugin_dir_url( __FILE__ ) . 'css/bootstrap-4.1.0/dist/css/bootstrap.full.css', array(), '1.0.0', 'all');
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in My_Wp_Tools_Loader as all of the hooks are defined
* in that particular class.
*
* The My_Wp_Tools_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/my-wp-tools-public.css', array(), $this->version, 'all' );
}
/**
* Register the JavaScript for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_scripts() {
/**
* Load Bootstrap.
*/
//wp_register_script('bootstrap-js', plugin_dir_url( __FILE__ ) . 'css/bootstrap-4.1.0/dist/js/bootstrap.full.js', array('jquery'), '3.3.1', true);
//wp_enqueue_script('bootstrap-js');
/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in My_Wp_Tools_Loader as all of the hooks are defined
* in that particular class.
*
* The My_Wp_Tools_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/my-wp-tools-public.js', array( 'jquery' ), $this->version, false );
}
/**
* Adds a grid with the most popular posts for today with a shortcode.
*/
function mwpt_most_popular_today($content) {
if ( is_home() || is_front_page() ) {
// Require the query result from the wordpress popular posts plugin.
require_once( ABSPATH . 'wp-content/plugins/wordpress-popular-posts/includes/class-wordpress-popular-posts-query.php');
// Query the database.
$popular_posts = new WPP_Query( array('range' => 'last7days', 'order_by' => 'views', 'limit' => 24) );
// Get the thumbnail url, the thumbnail alt, the post title of each post.
$ppop_post = [];
foreach($popular_posts->get_posts() as $item) {
$thumb_id = get_post_thumbnail_id($item->id);
$thumb_url = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
$thumb_alt = (get_post_meta($thumb_id, '_wp_attachment_image_alt', true) != '') ? get_post_meta($thumb_id, '_wp_attachment_image_alt', true) : $item->title;
// Create the object for each post.
$pop = new stdClass();
$pop->thumb_url = $thumb_url[0];
$pop->thumb_alt = $thumb_alt;
$pop->post_url = get_permalink($item->id);
$pop->post_title = $item->title;
array_push($ppop_post, $pop);
}
return $content . '<p><h3>Most Popular - Today</h3></p>' . $this->create_grid($ppop_post);
}
}
function create_grid($ppop_post) {
// Start: This code is to replace with a bootstrap grid.
$grid = '<table style="width:100%; table-layout: fixed;">';
$col = 1;
$max_col = 4;
$num_array_elements = 1;
foreach($ppop_post as $item) {
if ($col == 1) {
$grid .= '<tr>';
}
$grid .= '<td style="width: 25%;">' .
'<img src="' . $item->thumb_url . '" alt="' . $item->thumb_alt . '" height="130" width="130">' .
'<h4 style="font-size:17px;"><a href="' . $item->post_url . '"><b>' . $item->post_title . '</b></a></h4>' .
'</td>';
if ($col == $max_col || $num_array_elements == count($ppop_post)) {
$grid .= '</tr>';
$col = 1;
}
$col++;
$num_array_elements++;
}
$grid .= '</table>';
// End: This code is to replace with a bootstrap grid.
// html code to test the bootstrap integration.
$html = '<div class="alert alert-success"><strong>Success!</strong> This is a bootstrap alert message.</div>';
return $grid . '<br>' . $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment