1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Plugin Name: DummyBody |
4
|
|
|
* Plugin URI: https://dummybot.com |
5
|
|
|
* Description: Spin up test posts, pages, CPTs, and terms from an easy-to-use admin page. |
6
|
|
|
* Version: 1.0 |
7
|
|
|
* Author: Mike Selander |
8
|
|
|
* Author URI: https://github.com/mikeselander/ |
9
|
|
|
* Text Domain: dummybot |
10
|
|
|
* Domain Path: /languages |
11
|
|
|
* License: GPL2 |
12
|
|
|
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace DummyPress; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Autoloader callback. |
19
|
|
|
* |
20
|
|
|
* Converts a class name to a file path and requires it if it exists. |
21
|
|
|
* |
22
|
|
|
* @param string $class Class name. |
23
|
|
|
*/ |
24
|
|
|
function dummypress_autoloader( $class ) { |
25
|
|
|
$namespace = explode( '\\', $class ); |
26
|
|
|
|
27
|
|
|
if ( __NAMESPACE__ !== $namespace[0] ) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$class = str_replace( __NAMESPACE__ . '\\', '', $class ); |
32
|
|
|
|
33
|
|
|
$nss = array( |
34
|
|
|
'Abstracts', |
35
|
|
|
'Types', |
36
|
|
|
'Views' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
if ( in_array( $namespace[1], $nss ) ) { |
40
|
|
|
$class = strtolower( preg_replace( '/(?<!^)([A-Z])/', '/\1', $class ) ); |
41
|
|
|
$class = str_replace( '\\', '', $class ); |
42
|
|
|
$file = dirname( __FILE__ ) . '/' . $class . '.php'; |
43
|
|
|
} else { |
44
|
|
|
$class = strtolower( preg_replace( '/(?<!^)([A-Z])/', '-\\1', $class ) ); |
45
|
|
|
$file = dirname( __FILE__ ) . '/includes/class-' . $class . '.php'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ( is_readable( $file ) ) { |
49
|
|
|
require_once( $file ); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
spl_autoload_register( __NAMESPACE__ . '\dummypress_autoloader' ); |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Retrieve the plugin instance. |
58
|
|
|
* |
59
|
|
|
* @return object Plugin |
60
|
|
|
*/ |
61
|
|
|
function plugin() { |
62
|
|
|
static $instance; |
63
|
|
|
|
64
|
|
|
if ( null === $instance ) { |
65
|
|
|
$instance = new Plugin(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $instance; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Set our definitions for later use |
72
|
|
|
plugin()->set_definitions( |
73
|
|
|
(object) array( |
74
|
|
|
'basename' => plugin_basename( __FILE__ ), |
75
|
|
|
'directory' => plugin_dir_path( __FILE__ ), |
76
|
|
|
'file' => __FILE__, |
77
|
|
|
'slug' => 'test-content', |
78
|
|
|
'url' => plugin_dir_url( __FILE__ ) |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
// Register hook providers and views. |
83
|
|
|
plugin()->register_hooks( new AdminPage() ) |
84
|
|
|
->register_hooks( new Ajax() ) |
85
|
|
|
->register_view( new Views\Posts() ) |
86
|
|
|
->register_view( new Views\Terms() ) |
87
|
|
|
->register_view( new Views\Users() ) |
88
|
|
|
->register_view( new Views\Various() ) |
89
|
|
|
->register_type( new Types\Post() ) |
90
|
|
|
->register_type( new Types\Term() ) |
91
|
|
|
->register_type( new Types\User() ); |
92
|
|
|
|
93
|
|
|
// Load textdomain hook |
94
|
|
|
add_action( 'plugins_loaded', array( plugin(), 'load_textdomain' ) ); |
95
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.