Browse Source

Add a couple of custom widgets.

pull/3/head
Ned Zimmerman 7 years ago
parent
commit
0c6fbe022c
No known key found for this signature in database
GPG Key ID: FF56334A013120CA
  1. 54
      app/controllers/Home.php
  2. 20
      app/setup.php
  3. 8
      app/widgets.php
  4. 72
      app/widgets/linkbutton.php
  5. 81
      app/widgets/pagebutton.php
  6. 6
      dist/assets.json
  7. 0
      dist/scripts/customizer_ec2fc400.js
  8. 0
      dist/scripts/main_ec2fc400.js
  9. 2
      dist/styles/main_ec2fc400.css
  10. 25
      resources/assets/styles/components/_buttons.scss
  11. 5
      resources/assets/styles/layouts/_pages.scss
  12. 2
      resources/functions.php
  13. 16
      resources/views/index.blade.php

54
app/controllers/Home.php

@ -10,11 +10,11 @@ class Home extends Controller
{
$c = 0;
foreach ([
'home-block-one',
'home-block-two',
'home-block-three',
'home-block-four',
'home-block-five'
'home-block-1',
'home-block-2',
'home-block-3',
'home-block-4',
'home-block-5'
] as $block) {
if (is_active_sidebar($block)) {
$c++;
@ -23,53 +23,15 @@ class Home extends Controller
return $c;
}
public function home_blocks()
public function homeBlocks()
{
$blocks = [];
for ($i = 0; $i < 5; $i++) {
if ($i === 0) {
if (Home::getNextBlock()) {
$blocks[] = Home::getNextBlock();
}
} elseif ($i > 0) {
if (Home::getNextBlock($blocks[$i - 1])) {
$blocks[] = Home::getNextBlock($blocks[$i - 1]);
} else {
break;
}
if (is_active_sidebar("home-block-$i")) {
$blocks[] = "home-block-$i";
}
}
return $blocks;
}
public static function getNextBlock($current_block = null)
{
switch ($current_block) {
case 'home-block-one':
$next_block = 'home-block-two';
break;
case 'home-block-two':
$next_block = 'home-block-three';
break;
case 'home-block-three':
$next_block = 'home-block-four';
break;
case 'home-block-four':
$next_block = 'home-block-five';
break;
case 'home-block-five':
$next_block = null;
break;
default:
$next_block = 'home-block-one';
}
if (! $next_block) {
return false;
} elseif (is_active_sidebar($next_block)) {
return $next_block;
} else {
Home::getNextBlock($next_block);
}
}
}

20
app/setup.php

@ -85,24 +85,24 @@ add_action('widgets_init', function () {
'after_title' => '</h3>'
];
register_sidebar([
'name' => __('Home Block One', 'pressbooks-aldine'),
'id' => 'home-block-one'
'name' => __('Home Block 1', 'pressbooks-aldine'),
'id' => 'home-block-1'
] + $config);
register_sidebar([
'name' => __('Home Block Two', 'pressbooks-aldine'),
'id' => 'home-block-two'
'name' => __('Home Block 2', 'pressbooks-aldine'),
'id' => 'home-block-2'
] + $config);
register_sidebar([
'name' => __('Home Block Three', 'pressbooks-aldine'),
'id' => 'home-block-three'
'name' => __('Home Block 3', 'pressbooks-aldine'),
'id' => 'home-block-3'
] + $config);
register_sidebar([
'name' => __('Home Block Four', 'pressbooks-aldine'),
'id' => 'home-block-four'
'name' => __('Home Block 4', 'pressbooks-aldine'),
'id' => 'home-block-4'
] + $config);
register_sidebar([
'name' => __('Home Block Five', 'pressbooks-aldine'),
'id' => 'home-block-five'
'name' => __('Home Block 5', 'pressbooks-aldine'),
'id' => 'home-block-5'
] + $config);
});

8
app/widgets.php

@ -0,0 +1,8 @@
<?php
namespace App;
add_action('widgets_init', function () {
register_widget('Aldine\LinkButton');
register_widget('Aldine\PageButton');
});

72
app/widgets/linkbutton.php

@ -0,0 +1,72 @@
<?php
namespace Aldine;
class LinkButton extends \WP_Widget
{
/**
* Constructor.
*
* @see WP_Widget::__construct()
*
*/
public function __construct()
{
parent::__construct('linkbutton', __('Link Button', 'pressbooks-aldine'), [
'description' => esc_html__('Add a styled link button.', 'pressbooks-aldine')
]);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget($args, $instance)
{
echo $args['before_widget'];
if (! empty($instance['url']) && ! empty($instance['title'])) {
printf('<a class="button" href="%1$s">%2$s</a>', $instance['url'], apply_filters('widget_title', $instance['title']));
}
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form($instance)
{
$title = ! empty($instance['title']) ? $instance['title'] : '';
$url = ! empty($instance['url']) ? $instance['url'] : ''; ?>
<p><label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_attr_e('Title:', 'pressbooks-aldine'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>"></p>
<p><label for="<?php echo esc_attr($this->get_field_id('url')); ?>"><?php esc_attr_e('URL:', 'pressbooks-aldine'); ?></label>
<input class="widefat code" id="<?php echo esc_attr($this->get_field_id('url')); ?>" name="<?php echo esc_attr($this->get_field_name('url')); ?>" type="text" value="<?php echo esc_attr($url); ?>"></p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update($new_instance, $old_instance)
{
$instance = [];
$instance['title'] = ( ! empty($new_instance['title']) ) ? strip_tags($new_instance['title']) : '';
$instance['url'] = ( ! empty($new_instance['url']) ) ? esc_url($new_instance['url']) : '';
return $instance;
}
}

81
app/widgets/pagebutton.php

@ -0,0 +1,81 @@
<?php
namespace Aldine;
class PageButton extends \WP_Widget
{
/**
* Constructor.
*
* @see WP_Widget::__construct()
*
*/
public function __construct()
{
parent::__construct('pagebutton', __('Page Button', 'pressbooks-aldine'), [
'description' => esc_html__('Add a styled button which links to a page.', 'pressbooks-aldine')
]);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget($args, $instance)
{
echo $args['before_widget'];
if (! empty($instance['page_id'])) {
if (empty($instance['title'])) {
$instance['title'] = get_the_title($instance['page_id']);
}
printf('<a class="button" href="%1$s">%2$s</a>', get_permalink($instance['page_id']), apply_filters('widget_title', $instance['title']));
}
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form($instance)
{
$title = ! empty($instance['title']) ? $instance['title'] : '';
$url = ! empty($instance['page_id']) ? $instance['page_id'] : ''; ?>
<p><label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_attr_e('Title:', 'pressbooks-aldine'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>"></p>
<p><label for="<?php echo esc_attr($this->get_field_id('page_id')); ?>"><?php esc_attr_e('Page:', 'pressbooks-aldine'); ?></label>
<select id="<?php echo esc_attr($this->get_field_id('page_id')); ?>" name="<?php echo esc_attr($this->get_field_name('page_id')); ?>">
<option value="" <?php selected($instance['page_id'], ''); ?>> -- </a>
<?php $pages = get_pages();
foreach ($pages as $page) { ?>
<option value="<?= $page->ID; ?>" <?php selected(@$instance['page_id'], $page->ID); ?>><?= $page->post_title; ?></option>
<?php } ?>
</select></p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update($new_instance, $old_instance)
{
$instance = [];
$instance['title'] = ( ! empty($new_instance['title']) ) ? strip_tags($new_instance['title']) : '';
$instance['page_id'] = ( ! empty($new_instance['page_id']) ) ? absint($new_instance['page_id']) : '';
return $instance;
}
}

6
dist/assets.json vendored

@ -10,7 +10,7 @@
"vendor/fontawesome-webfont.ttf?v=4.7.0": "vendor/fontawesome-webfont_b06871f2.ttf",
"vendor/fontawesome-webfont.woff2?v=4.7.0": "vendor/fontawesome-webfont_af7ae505.woff2",
"vendor/fontawesome-webfont.woff?v=4.7.0": "vendor/fontawesome-webfont_fee66e71.woff",
"scripts/customizer.js": "scripts/customizer_4c93eef7.js",
"styles/main.css": "styles/main_4c93eef7.css",
"scripts/main.js": "scripts/main_4c93eef7.js"
"scripts/customizer.js": "scripts/customizer_ec2fc400.js",
"styles/main.css": "styles/main_ec2fc400.css",
"scripts/main.js": "scripts/main_ec2fc400.js"
}

0
dist/scripts/customizer_4c93eef7.js → dist/scripts/customizer_ec2fc400.js vendored

0
dist/scripts/main_4c93eef7.js → dist/scripts/main_ec2fc400.js vendored

2
dist/styles/main_4c93eef7.css → dist/styles/main_ec2fc400.css vendored

File diff suppressed because one or more lines are too long

25
resources/assets/styles/components/_buttons.scss

@ -0,0 +1,25 @@
.button {
display: inline-block;
width: 250px;
height: 60px;
margin-top: 2em;
padding: 1.25em;
border: 2px solid $brand-primary;
border-radius: 3px;
background: $brand-primary;
font-family: Karla, sans-serif;
font-size: 16px;
font-weight: 600;
color: $white;
letter-spacing: 2px;
text-align: center;
text-decoration: none;
text-transform: uppercase;
transition: all 0.5s;
&:hover,
&:focus {
background: $white;
color: $brand-primary;
}
}

5
resources/assets/styles/layouts/_pages.scss

@ -38,6 +38,11 @@
.inside {
width: 100%;
}
.widget_linkbutton,
.widget_pagebutton {
text-align: center;
}
}
.one-two {

2
resources/functions.php

@ -58,7 +58,7 @@ array_map(function ($file) use ($sage_error) {
if (!locate_template($file, true, true)) {
$sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'pressbooks-aldine'), $file), 'File not found');
}
}, ['helpers', 'setup', 'filters', 'admin']);
}, ['helpers', 'setup', 'filters', 'admin', 'widgets', 'widgets/linkbutton', 'widgets/pagebutton']);
/**
* Here's what's happening with these hooks:

16
resources/views/index.blade.php

@ -1,7 +1,14 @@
@extends('layouts.app')
@section('content')
@if($block_count < 5)
@if($block_count === 0)
<div class="block block-1">
<div class="inside">
<h3>{{ __('About Pressbooks', 'pressbooks-aldine')}}</h3>
<p>{{ __('Pressbooks is easy-to-use book writing software that lets you create a book in all the formats you need to publish.', 'pressbooks-aldine')}}</p>
</div>
</div>
@elseif($block_count < 5)
@for($i = 0; $i < $block_count; $i++)
<div class="block block-{{ $i + 1 }}">
<div class="inside">
@ -26,12 +33,5 @@
</div>
</div>
@endfor
@else
<div class="block block-1">
<div class="inside">
<h3>{{ __('About Pressbooks', 'pressbooks-aldine')}}</h3>
<p>{{ __('Pressbooks is easy-to-use book writing software that lets you create a book in all the formats you need to publish.', 'pressbooks-aldine')}}</p>
</div>
</div>
@endif
@endsection

Loading…
Cancel
Save