Skip to content

Commit

Permalink
Merge pull request #1 from ohryan/update-to-blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ohryan authored Aug 15, 2023
2 parents 2186347 + c5ee6b6 commit 64660ad
Show file tree
Hide file tree
Showing 20 changed files with 31,792 additions and 109 deletions.
25 changes: 25 additions & 0 deletions build/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "ohryan-historian/historian-block",
"version": "0.1.0",
"title": "Historian Block",
"category": "widgets",
"icon": "calendar-alt",
"description": "Historian surfaces old blog posts on your dashboard and as a sidebar widget.",
"supports": {
"html": false
},
"attributes": {
"title": {
"type": "string",
"source": "html",
"selector": "h3",
"default": "This week in history"
}
},
"textdomain": "ohryan-historian",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css"
}
1 change: 1 addition & 0 deletions build/index.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-element', 'wp-url'), 'version' => '767ee09a92d7feb25772');
1 change: 1 addition & 0 deletions build/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.wp-block-create-block-starter-block{border:1px dotted red}
1 change: 1 addition & 0 deletions build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build/style-index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.wp-block-create-block-starter-block{background-color:#21759b;color:#fff;padding:2px}
75 changes: 75 additions & 0 deletions classes/Historian-class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace Ohryan\Historian;
use \WP_Query;

class Historian {


/**
* Get all posts by week.
*
* @return WP_Query
*/
private function get_posts_for_this_week_of_the_year() {
return new WP_Query(array(
'posts_per_page' => 25, // @todo perhaps make this configurable
'post_status' => 'publish',
'w' => date('W'),
'date_query' => array( 'before' => date( 'Y-m-d 00:00' ),)
));

}


/**
* Group posts by year.
*
* @param WP_Query $posts
*
* @return array
*/
private function group_posts_by_year($posts) {

$output = array();

if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();

$post_year = get_the_date('Y');
$output[$post_year][] = array(
'permalink' => get_the_permalink(),
'title' => get_the_title(),
);
}
}

return $output;
}

/**
* Output the widget.
*
* @return void
*/
public function display_legacy_widget() {
echo $this->get_widget_content();
}

public function get_widget_content(){
$output = '';
$posts = $this->get_posts_for_this_week_of_the_year();
$posts_by_year = $this->group_posts_by_year( $posts );

foreach ( $posts_by_year as $year => $posts ) {
$output .= "<h4>$year</h4>";
$output .= '<ul>';
array_walk($posts, function($post) use (&$output) {
$output .= "<li><a href='{$post['permalink']}'>{$post['title']}</a></li>";
});
$output .= '</ul>';
}

return $output;
}
}
67 changes: 0 additions & 67 deletions classes/Historian.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php
namespace RP;
use \RP\Historian;
namespace Ohryan\Historian;
use \WP_Query;

class HistorianWidget extends \WP_Widget {

function __construct() {
parent::WP_Widget('Historian_Widget',
parent::__construct('Historian_Widget',
$name = __('Historian', 'rp_historian_plugin' ),
array( 'description' => __('Posts from this week in your site\'s history.', 'wp_historian_plugin' )));
}
Expand Down Expand Up @@ -39,7 +38,7 @@ function widget( $args, $instance ) {

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

echo $h->displaySidebarWidgetList();
$h->display_legacy_widget();

echo '</div>'.$after_widget;
}
Expand Down
47 changes: 47 additions & 0 deletions historian.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
Plugin Name: Historian Block
Plugin URI: https://github.com/ohryan/RetroPosts
Description: Historian Block provides a "this day in history" for your blog.
Version: 2.0
Author: Ryan Neudorf
Author URI: http://ohryan.ca/
Text Domain: ohryan-historian
License: GPLv2 or later
*/
namespace Ohryan\Historian;

require_once 'classes/Historian-class.php';
require_once 'classes/HistorianWidget-class.php';

function historian_add_dashboard_widgets() {
wp_add_dashboard_widget(
'retroposts_dashboard_widget',
'Historian',
__NAMESPACE__.'\\historian_dashboard_widget_display'
);
}

add_action( 'wp_dashboard_setup', __NAMESPACE__.'\\historian_add_dashboard_widgets' );

function historian_dashboard_widget_display() {
$h = new Historian();
$h->display_legacy_widget();
}

add_action('widgets_init', function(){ return register_widget(__NAMESPACE__."\\HistorianWidget"); });

function historian_block_render_callback($attributes, $content, $block) {
return $content . (new Historian())->get_widget_content();
}

function historian_block_init() {
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => __NAMESPACE__ . "\\historian_block_render_callback"
)
);
}

add_action( 'init', __NAMESPACE__ . "\\historian_block_init" );
Loading

0 comments on commit 64660ad

Please sign in to comment.