Completed
Push — master ( 13b33c...3346fb )
by Ahmad
03:16
created

TitanFrameworkChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
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 37 and the first side effect is on line 143.

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
 * This script is not used within Titan Framework itself.
4
 *
5
 * This script is meant to be used with your Titan Framework-dependent theme or plugin,
6
 * so that your theme/plugin can verify whether the framework is installed.
7
 *
8
 * If Titan is not installed, then the script will display a notice with a link to
9
 * Titan. If Titan is installed but not activated, it will display the appropriate notice as well.
10
 *
11
 * To use this script, just copy it into your theme/plugin directory then add this in the main file of your project:
12
 *
13
 * require_once( 'titan-framework-checker.php' );
14
 *
15
 * Changelog:
16
 * v1.9
17
 *      * Simplified class
18
 * v1.7.4
19
 *		* Now integrates with TGM Plugin Activation - uses TGM instead of displaying
20
 *			our own admin notice
21
 * v1.7.7
22
 *		* Added filters to notices
23
 *
24
 * @package Titan Framework
25
 */
26
27
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly.
28
}
29
30
if ( ! class_exists( 'TitanFrameworkChecker' ) ) {
31
32
	/**
33
	 * Titan Framework Checker.
34
	 *
35
	 * @since 1.6
36
	 */
37
	class TitanFrameworkChecker {
38
39
40
		const SEARCH_REGEX = '/titan-framework.php/i';
41
		const TITAN_CLASS = 'TitanFramework';
42
		const PLUGIN_SLUG = 'titan-framework';
43
44
45
		/**
46
		 * Constructor, add hooks for checking for Titan Framework.
47
		 *
48
		 * @since 1.6
49
		 */
50
		function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
			add_action( 'admin_notices', array( $this, 'display_install_or_active_notice' ), 2 );
52
			add_action( 'tgmpa_register', array( $this, 'tgm_plugin_activation_include' ) );
53
		}
54
55
56
		/**
57
		 * Checks the existence of Titan Framework and prompts the display of a notice.
58
		 *
59
		 * @since 1.6
60
		 */
61
		public function display_install_or_active_notice() {
62
63
			// Check for TGM use, if used, let TGM do the notice.
64
			// We do this here since perform_check() is too early.
65
			if ( function_exists( 'tgmpa' ) ) {
66
				return;
67
			}
68
69
			// If the plugin does not exist, throw admin notice to install.
70
			if ( ! $this->plugin_exists() ) {
71
				echo "<div class='error'><p><strong>"
72
					. esc_html( apply_filters( 'titan_checker_installation_notice', __( 'Titan Framework needs to be installed.', 'default' ) ) )
73
					. sprintf( " <a href='%s'>%s</a>",
74
						esc_url( admin_url( 'plugin-install.php?tab=search&type=term&s=titan+framework' ) ),
75
						esc_html( apply_filters( 'titan_checker_search_plugin_notice', __( 'Click here to search for the plugin.', 'default' ) ) )
76
					)
77
					. '</strong></p></div>';
78
79
				// If the class doesn't exist, the plugin is inactive. Throw admin notice to activate plugin.
80
			} else if ( ! class_exists( apply_filters( 'tf_framework_checker_titan_class', self::TITAN_CLASS ) ) ) {
81
				echo "<div class='error'><p><strong>"
82
					. esc_html( apply_filters( 'titan_checker_activation_notice', __( 'Titan Framework needs to be activated.', 'default' ) ) )
83
					. sprintf( " <a href='%s'>%s</a>",
84
						esc_url( admin_url( 'plugins.php' ) ),
85
						esc_html( apply_filters( 'titan_checker_activate_plugin_notice', __( 'Click here to go to the plugins page and activate it.', 'default' ) ) )
86
					)
87
					. '</strong></p></div>';
88
			}
89
		}
90
91
92
		/**
93
		 * Checks the existence of Titan Framework in the list of plugins.
94
		 * It uses the slug path of the plugin for checking.
95
		 *
96
		 * @since 1.6
97
		 *
98
		 * @return boolean True if Titan Framework is installed (even if not activated).
99
		 */
100
		public function plugin_exists() {
101
			// Required function as it is only loaded in admin pages.
102
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
103
104
			// Get all plugins, activated or not.
105
			$plugins = get_plugins();
106
107
			// Check plugin existence by checking if the name is registered as an array key. get_plugins collects all plugin path into arrays.
108
			foreach ( $plugins as $slug => $plugin ) {
109
				$searchRegex = apply_filters( 'tf_framework_checker_regex', self::SEARCH_REGEX );
110
				if ( preg_match( $searchRegex, $slug, $matches ) ) {
111
					return true;
112
				}
113
			}
114
115
			return false;
116
		}
117
118
119
		/**
120
		 * Includes Titan Framework in TGM Plugin Activation if it's available.
121
		 *
122
		 * @since	1.7.4
123
		 *
124
		 * @return	void
125
		 *
126
		 * @see		http://tgmpluginactivation.com/
127
		 *
128
		 * @codeCoverageIgnore
129
		 */
130
		public function tgm_plugin_activation_include() {
131
			if ( function_exists( 'tgmpa' ) ) {
132
			    tgmpa( array(
133
			        array(
134
			            'name' => 'Titan Framework',
135
			            'slug' => self::PLUGIN_SLUG,
136
			            'required' => true,
137
			        ),
138
			    ) );
139
			}
140
		}
141
	}
142
143
	new TitanFrameworkChecker();
144
145
146
}
147