Completed
Push — master ( ef04cf...199149 )
by Mike
02:19
created

test-content.php ➔ test_content_autoloader()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 3
b 0
f 0
nc 5
nop 1
dl 0
loc 23
rs 8.7972
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: Old Town Media
8
 * Author URI: https://github.com/oldtownmedia/
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
34
	if ( 'Views' === $namespace[1] ){
35
        $class = strtolower( preg_replace( '/(?<!^)([A-Z])/', '/\1', $class ) );
36
        $class = str_replace( '\\', '', $class );
37
     	$file  = dirname( __FILE__ ) . '/' . $class . '.php';
38
    } else {
39
        $class = strtolower( preg_replace( '/(?<!^)([A-Z])/', '-\\1', $class ) );
40
     	$file  = dirname( __FILE__ ) . '/includes/class-' . $class . '.php';
41
    }
42
43
 	if ( file_exists( $file ) ) {
44
 		require_once( $file );
45
 	}
46
 }
47
 spl_autoload_register( __NAMESPACE__ . '\test_content_autoloader' );
48
49
50
51
 /**
52
  * Retrieve the plugin instance.
53
  *
54
  * @return object Plugin
55
  */
56
 function plugin() {
57
 	static $instance;
58
59
 	if ( null === $instance ) {
60
 		$instance = new Plugin();
61
 	}
62
63
 	return $instance;
64
 }
65
66
// Set our definitions for later use
67
 plugin()->set_definitions(
68
	(object) array(
69
		'basename'	=> plugin_basename( __FILE__ ),
70
		'directory'	=> plugin_dir_path( __FILE__ ),
71
		'file'		=> __FILE__,
72
		'slug' 		=> 'structure',
73
		'url'		=> plugin_dir_url( __FILE__ )
74
	)
75
);
76
77
 // Register hook providers and views.
78
 plugin()->register_hooks( new AdminPage() )
79
         ->register_views( new Views\Posts() )
80
         ->register_views( new Views\Terms() )
81
		 ->register_views( new Views\Various() );
82
83
// Load textdomain hook
84
add_action( 'plugins_loaded', array( plugin(), 'load_textdomain' ) );
85