Skip to content

Commit

Permalink
Lesson 13
Browse files Browse the repository at this point in the history
  • Loading branch information
Alecaddd committed Oct 25, 2017
1 parent d9cc445 commit 14981a1
Show file tree
Hide file tree
Showing 23 changed files with 854 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Lesson13/alecaddd-plugin.php
@@ -0,0 +1,63 @@
<?php
/**
* @package AlecadddPlugin
*/
/*
Plugin Name: Alecaddd Plugin
Plugin URI: http://alecaddd.com/plugin
Description: This is my first attempt on writing a custom Plugin for this amazing tutorial series.
Version: 1.0.0
Author: Alessandro "Alecaddd" Castellani
Author URI: http://alecaddd.com
License: GPLv2 or later
Text Domain: alecaddd-plugin
*/

/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Copyright 2005-2015 Automattic, Inc.
*/

// If this file is called firectly, abort!!!
defined( 'ABSPATH' ) or die( 'Hey, what are you doing here? You silly human!' );

// Require once the Composer Autoload
if ( file_exists( dirname( __FILE__ ) . '/vendor/autoload.php' ) ) {
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
}

/**
* The code that runs during plugin activation
*/
function activate_alecaddd_plugin() {
Inc\Base\Activate::activate();
}
register_activation_hook( __FILE__, 'activate_alecaddd_plugin' );

/**
* The code that runs during plugin deactivation
*/
function deactivate_alecaddd_plugin() {
Inc\Base\Deactivate::deactivate();
}
register_deactivation_hook( __FILE__, 'deactivate_alecaddd_plugin' );

/**
* Initialize all the core classes of the plugin
*/
if ( class_exists( 'Inc\\Init' ) ) {
Inc\Init::register_services();
}
Empty file added Lesson13/assets/myscript.js
Empty file.
Empty file added Lesson13/assets/mystyle.css
Empty file.
17 changes: 17 additions & 0 deletions Lesson13/composer.json
@@ -0,0 +1,17 @@
{
"name": "alecaddd/alecaddd-plugin",
"description": "Awesome starter plugin example",
"type": "project",
"license": "GPL",
"authors": [
{
"name": "Alecaddd",
"email": "castellani.ale@gmail.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {"Inc\\": "./inc"}
}
}
12 changes: 12 additions & 0 deletions Lesson13/inc/Base/Activate.php
@@ -0,0 +1,12 @@
<?php
/**
* @package AlecadddPlugin
*/
namespace Inc\Base;

class Activate
{
public static function activate() {
flush_rewrite_rules();
}
}
20 changes: 20 additions & 0 deletions Lesson13/inc/Base/BaseController.php
@@ -0,0 +1,20 @@
<?php
/**
* @package AlecadddPlugin
*/
namespace Inc\Base;

class BaseController
{
public $plugin_path;

public $plugin_url;

public $plugin;

public function __construct() {
$this->plugin_path = plugin_dir_path( dirname( __FILE__, 2 ) );
$this->plugin_url = plugin_dir_url( dirname( __FILE__, 2 ) );
$this->plugin = plugin_basename( dirname( __FILE__, 3 ) ) . '/alecaddd-plugin.php';
}
}
12 changes: 12 additions & 0 deletions Lesson13/inc/Base/Deactivate.php
@@ -0,0 +1,12 @@
<?php
/**
* @package AlecadddPlugin
*/
namespace Inc\Base;

class Deactivate
{
public static function deactivate() {
flush_rewrite_rules();
}
}
23 changes: 23 additions & 0 deletions Lesson13/inc/Base/Enqueue.php
@@ -0,0 +1,23 @@
<?php
/**
* @package AlecadddPlugin
*/
namespace Inc\Base;

use \Inc\Base\BaseController;

/**
*
*/
class Enqueue extends BaseController
{
public function register() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
}

function enqueue() {
// enqueue all our scripts
wp_enqueue_style( 'mypluginstyle', $this->plugin_url . 'assets/mystyle.css' );
wp_enqueue_script( 'mypluginscript', $this->plugin_url . 'assets/myscript.js' );
}
}
22 changes: 22 additions & 0 deletions Lesson13/inc/Base/SettingsLinks.php
@@ -0,0 +1,22 @@
<?php
/**
* @package AlecadddPlugin
*/
namespace Inc\Base;

use \Inc\Base\BaseController;

class SettingsLinks extends BaseController
{
public function register()
{
add_filter( "plugin_action_links_$this->plugin", array( $this, 'settings_link' ) );
}

public function settings_link( $links )
{
$settings_link = '<a href="admin.php?page=alecaddd_plugin">Settings</a>';
array_push( $links, $settings_link );
return $links;
}
}
48 changes: 48 additions & 0 deletions Lesson13/inc/Init.php
@@ -0,0 +1,48 @@
<?php
/**
* @package AlecadddPlugin
*/
namespace Inc;

final class Init
{
/**
* Store all the classes inside an array
* @return array Full list of classes
*/
public static function get_services()
{
return [
Pages\Admin::class,
Base\Enqueue::class,
Base\SettingsLinks::class
];
}

/**
* Loop through the classes, initialize them,
* and call the register() method if it exists
* @return
*/
public static function register_services()
{
foreach ( self::get_services() as $class ) {
$service = self::instantiate( $class );
if ( method_exists( $service, 'register' ) ) {
$service->register();
}
}
}

/**
* Initialize the class
* @param class $class class from the services array
* @return class instance new instance of the class
*/
private static function instantiate( $class )
{
$service = new $class();

return $service;
}
}
25 changes: 25 additions & 0 deletions Lesson13/inc/Pages/Admin.php
@@ -0,0 +1,25 @@
<?php
/**
* @package AlecadddPlugin
*/
namespace Inc\Pages;

use \Inc\Base\BaseController;

/**
*
*/
class Admin extends BaseController
{
public function register() {
add_action( 'admin_menu', array( $this, 'add_admin_pages' ) );
}

public function add_admin_pages() {
add_menu_page( 'Alecaddd Plugin', 'Alecaddd', 'manage_options', 'alecaddd_plugin', array( $this, 'admin_index' ), 'dashicons-store', 110 );
}

public function admin_index() {
require_once $this->plugin_path . 'templates/admin.php';
}
}
2 changes: 2 additions & 0 deletions Lesson13/index.php
@@ -0,0 +1,2 @@
<?php
// Silence is golden.
1 change: 1 addition & 0 deletions Lesson13/templates/admin.php
@@ -0,0 +1 @@
<h1>Alecaddd Plugin</h1>
24 changes: 24 additions & 0 deletions Lesson13/uninstall.php
@@ -0,0 +1,24 @@
<?php

/**
* Trigger this file on Plugin uninstall
*
* @package AlecadddPlugin
*/

if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
die;
}

// Clear Database stored data
$books = get_posts( array( 'post_type' => 'book', 'numberposts' => -1 ) );

foreach( $books as $book ) {
wp_delete_post( $book->ID, true );
}

// Access the database via SQL
global $wpdb;
$wpdb->query( "DELETE FROM wp_posts WHERE post_type = 'book'" );
$wpdb->query( "DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts)" );
$wpdb->query( "DELETE FROM wp_term_relationships WHERE object_id NOT IN (SELECT id FROM wp_posts)" );
7 changes: 7 additions & 0 deletions Lesson13/vendor/autoload.php
@@ -0,0 +1,7 @@
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit51b523f56e298134d109fbc73bbc4aba::getLoader();

0 comments on commit 14981a1

Please sign in to comment.