test-content.php ➔ test_content_autoloader()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 4
eloc 18
c 5
b 0
f 1
nc 5
nop 1
dl 0
loc 28
rs 8.5806
1
<?php
2
/**
3
 * Plugin Name: Test Content Generator
4
 * Plugin URI: https://github.com/oldtownmedia/test-content-suite
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: otm-test-content
10
 * Domain Path: /languages
11
 * License: GPL2
12
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
13
 */
14
15
namespace testContent;
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 test_content_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__ . '\test_content_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\Various() )
88
		 ->register_type( new Types\Post() )
89
		 ->register_type( new Types\Term() );
90
91
// Load textdomain hook
92
add_action( 'plugins_loaded', array( plugin(), 'load_textdomain' ) );
93