Skip to content

Commit

Permalink
use plugin component
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardBock committed Jul 19, 2021
1 parent 184d211 commit 7a1e6f3
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 41 deletions.
57 changes: 21 additions & 36 deletions public/additional-authors.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
* Author URI: https://palasthotel.de
* Text Domain: additional-authors
* Domain Path: /languages
* Version: 1.2.10
* Version: 1.2.11
* Requires at least: 5.0
* Tested up to: 5.7.1
* Tested up to: 5.8
*/

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

/**
* feature classes
*/
require_once dirname(__FILE__)."/vendor/autoload.php";

/**
* Class AdditionalAuthors
* @property Database database
Expand All @@ -32,10 +37,8 @@
* @property REST rest
* @property Gutenberg gutenberg
* @property PostsTable postsTable
* @property string path
* @property string url
*/
class Plugin {
class Plugin extends Components\Plugin {

const DOMAIN = "additional-authors";

Expand Down Expand Up @@ -90,41 +93,19 @@ class Plugin {
const META_POST_ADDITIONAL_AUTHORS = "_additional_authors";
const META_USER_GENERATED = "_additional_authors_generated_user";

private static $instance;

static function get_instance() {
if ( self::$instance == null ) {
self::$instance = new Plugin();
}

return self::$instance;
}

/**
* AdditionalAuthors constructor.
*/
function __construct() {
function onCreate() {

/**
* load translations
*/
load_plugin_textdomain(
$this->loadTextdomain(
self::DOMAIN,
false,
plugin_basename( dirname( __FILE__ ) ) . '/languages'
'languages'
);

/**
* base paths
*/
$this->path = plugin_dir_path( __FILE__ );
$this->url = plugin_dir_url( __FILE__ );

/**
* feature classes
*/
require_once dirname(__FILE__)."/vendor/autoload.php";

$this->database = new Database();
$this->query_manipulation = new QueryManipulation( $this );
$this->assets = new Assets($this);
Expand All @@ -139,10 +120,6 @@ function __construct() {

$this->update = new Update( $this );

/**
* on activate or deactivate plugin
*/
register_activation_hook(__FILE__, array($this, "activation"));
}

/**
Expand Down Expand Up @@ -176,16 +153,24 @@ public function generatePassword() {
/**
* on plugin activation
*/
function activation(){
function onSiteActivation(){
$this->database->install();
}

/**
* @deprecated use instance() instead
* @return Plugin|mixed
*/
public static function get_instance(){
return static::instance();
}

}

/**
* init plugin and make it accessible
*/
Plugin::get_instance();
Plugin::instance();

require_once dirname( __FILE__ ) . "/public-functions.php";
require_once dirname( __FILE__ ) . "/deprecated.php";
2 changes: 1 addition & 1 deletion public/classes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function enqueueGutenberg(){
apply_filters(
Plugin::FILTER_META_BOX_GET_USERS,
array(
'role__in' => 'authors',
'role__in' => ['authors', 'editor', 'administrator'],
'orderby' => 'display_name',
'fields' => array(
'ID',
Expand Down
117 changes: 117 additions & 0 deletions public/classes/Components/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace AdditionalAuthors\Components;

use ReflectionClass;
use ReflectionException;

/**
* @property string path
* @property string url
* @property string basename
* @version 0.1.3
*/
abstract class Plugin {

/**
* @var ReflectionClass
*/
private $ref;

private $tooLateForTextdomain;

/**
* @throws ReflectionException
*/
public function __construct() {
$this->ref = new ReflectionClass( get_called_class() );
$this->path = plugin_dir_path( $this->ref->getFileName() );
$this->url = plugin_dir_url( $this->ref->getFileName() );
$this->basename = plugin_basename( $this->ref->getFileName() );

$this->tooLateForTextdomain = false;
$this->onCreate();
$this->tooLateForTextdomain = true;

register_activation_hook( $this->ref->getFileName(), array( $this, "onActivation" ) );
register_deactivation_hook( $this->ref->getFileName(), array( $this, "onDeactivation" ) );

}

// -----------------------------------------------------------------------------
// lifecycle methods
// -----------------------------------------------------------------------------
abstract function onCreate();

public function onActivation( $networkWide ) {
if ( $networkWide ) {
$this->foreachMultisite( [ $this, 'onSiteActivation' ] );
} else {
$this->onSiteActivation();
}
}

public function onSiteActivation() {

}

public function onDeactivation( $networkWide ) {
if ( $networkWide ) {
$this->foreachMultisite( [ $this, 'onSiteDeactivation' ] );
} else {
$this->onSiteDeactivation();
}
}

public function onSiteDeactivation() {

}

// -----------------------------------------------------------------------------
// utility methods
// -----------------------------------------------------------------------------
public function loadTextdomain( string $domain, string $relativeLanguagesPath ) {
if ( $this->tooLateForTextdomain ) {
error_log( "Too late: You need to setTextdomain in onCreate Method of the Plugin class." );
return;
}
add_action( 'init', function () use ( $domain, $relativeLanguagesPath ) {
load_plugin_textdomain(
$domain,
false,
dirname( plugin_basename( $this->ref->getFileName() ) ) . "/" . $relativeLanguagesPath
);
} );
}

public function foreachMultisite(callable $onSite){
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
$network_site = get_network()->site_id;
$args = array( 'fields' => 'ids' );
$site_ids = get_sites( $args );

// run the activation function for each blog id
foreach ( $site_ids as $site_id ) {
switch_to_blog( $site_id );
$onSite();
}

// switch back to the network site
switch_to_blog( $network_site );
}
}

// -----------------------------------------------------------------------------
// singleton pattern
// -----------------------------------------------------------------------------
private static $instances = [];

public static function instance() {
$class = get_called_class();
if ( ! isset( self::$instances[ $class ] ) ) {
self::$instances[ $class ] = new static();
}

return self::$instances[ $class ];
}
}
2 changes: 1 addition & 1 deletion public/inc/migrate.additional-author-destination.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function save($item)
strtolower(preg_replace("/[^A-Za-z0-9 ]/","",$item->last_name));
}

$additional_authors = AdditionalAuthors\Plugin::get_instance();
$additional_authors = additional_authors_get_plugin();
$item->user_email = $item->user_login."@localhost.local";
$item->user_nicename = $item->user_login;
$unguessable_string = $additional_authors->generateUnguessableString();
Expand Down
2 changes: 1 addition & 1 deletion public/public-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @return Plugin
*/
function additional_authors_get_plugin(){
return Plugin::get_instance();
return Plugin::instance();
}

/**
Expand Down
7 changes: 5 additions & 2 deletions public/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Contributors: palasthotel, edwardbock, greatestview
Donate link: http://palasthotel.de/
Tags: author, meta fields
Requires at least: 4.0
Tested up to: 5.7.1
Stable tag: 1.2.10
Tested up to: 5.8
Stable tag: 1.2.11
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl

Expand All @@ -28,6 +28,9 @@ Let's you add more than one author to your posts.

== Changelog ==

= 1.2.11 =
* Optimization: Add admins and editors to additional author dropdown

= 1.2.10 =
* Bugfix: Additional authors script broke reusable block editor

Expand Down

0 comments on commit 7a1e6f3

Please sign in to comment.