Completed
Pull Request — master (#52)
by Bui Quang
03:48
created

WPDFI   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
dl 0
loc 78
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializes() 0 6 1
A hooks() 0 9 1
A install() 0 9 2
A init() 0 4 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 18.

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.

Loading history...
2
/**
3
 * Plugin Name: WP Default Feature Image.
4
 * Description: Help you choose your default thumbnail for post types, categories, tags,...
5
 * Version: 1.0.0
6
 * Author: Duc Bui Quang <[email protected]>
7
 * Author URI: https://www.ducbuiquang.com
8
 * License: GNUv3
9
 * Text Domain: wpdfi
10
 */
11
define('WPDFI_PLUGIN', __FILE__ );
12
define('WPDFI_PLUGIN_BASENAME', plugin_basename( WPDFI_PLUGIN ) );
13
define('WPDFI_URL_BASE', plugin_dir_url( WPDFI_PLUGIN) );
14
define('WPDFI_DIR_BASE', plugin_dir_path( WPDFI_PLUGIN ) );
15
define('WPDFI_ASSETS', WPDFI_URL_BASE . '/assets/' );
16
define('WPDFI_TEMPLATES_PATH', WPDFI_DIR_BASE.  '/templates/');
17
18
require_once WPDFI_DIR_BASE . '/vendor/autoload.php';
19
20
use WPDFI\Traits\HasModule;
21
use WPDFI\Traits\Singleton;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Singleton.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
22
use WPDFI\PostType;
23
use WPDFI\Taxonomy;
24
use WPDFI\Term;
25
use WPDFI\Ajax;
26
use WPDFI\Admin;
27
use WPDFI\Image;
28
use WPDFI\Layout;
29
use WPDFI\Admin\Notice;
30
31
final class WPDFI
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
{
33
	use HasModule;
34
	use Singleton;
35
	
36
	/**
37
	 * @traitDoc
38
	 */
39
	public function initializes() 
40
	{
41
		$this->loadModules();
42
43
		Ajax::instance();
44
	}
45
46
	/**
47
	 * All WordPress hooks come here
48
	 * 
49
	 * @since 1.0.0
50
	 * @return void
51
	 */
52
	public function hooks() 
53
	{
54
55
		add_action( 'init', [$this, 'init']);
56
57
		/* Load all module hooks */
58
		$this->moduleHooks();
59
60
	}
61
62
	/**
63
	 * All Install and default settings stuff for this plugin come here.
64
	 *
65
	 * @since 1.0.0
66
	 * @return void
67
	 */
68
	public function install() {
69
70
		$options = get_option('wpdfi-settings');
71
		if(!$options['options']['status_for_update']) {
72
			$options['options']['status_for_update'] = 'publish';
73
			update_option('wpdfi-settings', $options);
74
		}
75
76
	}
77
	
78
	/**
79
	 * init actions
80
	 * 
81
	 * @since 1.0.0
82
	 * @return void
83
	 */
84
	public function init() 
85
	{
86
		load_plugin_textdomain('wpdfi', false, WPDFI_DIR_BASE . '/lang/');
87
	}
88
	
89
	/**
90
	 * @traitDoc
91
	 */
92
	public function loadModules() {
93
		$modules = [
94
			'templater'	=> new VA\Templater(WPDFI_TEMPLATES_PATH, 'blade'),
95
			'post_type' => PostType::instance(),
96
			'taxonomy'	=> Taxonomy::instance(),
97
			'term'		=> Term::instance(),
98
			'image'		=> Image::instance(),
99
			'layout'	=> Layout::instance(),
100
			'admin_notice' => Notice::instance(),
101
			'admin'		=> Admin::instance()
102
		];
103
			
104
		foreach($modules as $moduleName => $moduleHandle) {
105
			$this->module($moduleName, $moduleHandle);
106
		}
107
		
108
		return $this;
109
	}
110
	
111
}
112
/**
113
 * Return singleton of WPDFI
114
 *
115
 * @since  1.0.0
116
 * @return WPDFI  Singleton instance of plugin class.
117
 */
118
function wpdfi() {
119
    return WPDFI::instance();
120
}
121
122
add_action('plugins_loaded', [wpdfi(), 'hooks']);
123
124
register_activation_hook(WPDFI_PLUGIN, [wpdfi(), 'install']);