Skip to content

Commit 80f503f

Browse files
committed
Create base plugin class with install script
1 parent 676c518 commit 80f503f

File tree

5 files changed

+123
-9
lines changed

5 files changed

+123
-9
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
},
3232
"scripts": {
3333
"refresh" : "git clean -xffd && composer install --ansi",
34-
"install-phpcs-rules" : "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run",
34+
"install:phpcs-rules" : "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run",
35+
"install:wptests": "bin/install-wp-tests.sh plugin_testing_tests root 'root' 127.0.0.1:8889 latest true",
36+
"fix:php" : "./vendor/bin/phpcbf wordpress-plugin-template.php ./src",
3537
"lint:php" : "./vendor/bin/phpcs ./ --extensions=php --ignore=vendor/,tests/",
36-
"fix:php" : "./vendor/bin/phpcbf ./inc && ./vendor/bin/phpcbf ./src",
37-
"test:php": "./vendor/bin/phpunit --colors=always tests",
38-
"test:php-testdox": "@test:php --testdox",
39-
"wp:install-tests": "bin/install-wp-tests.sh wordpress_unit_tests root 'root' 127.0.0.1:8889 latest true"
38+
"test:php": "./vendor/bin/phpunit --colors=always",
39+
"test:php-testdox": "@test:php --testdox"
4040
}
4141
}

phpcs.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<!-- Rules: Check PHP version compatibility -->
2020
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
21-
<config name="testVersion" value="5.3-"/>
21+
<config name="testVersion" value="7.4-"/>
2222
<!-- https://github.com/PHPCompatibility/PHPCompatibilityWP -->
2323
<rule ref="PHPCompatibilityWP"/>
2424

@@ -29,13 +29,13 @@
2929
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
3030
<properties>
3131
<!-- Value: replace the function, class, and variable prefixes used. Separate multiple prefixes with a comma. -->
32-
<property name="prefixes" type="array" value="wordpress-plugin-template"/>
32+
<property name="prefixes" type="array" value="wpt,WPT"/>
3333
</properties>
3434
</rule>
3535
<rule ref="WordPress.WP.I18n">
3636
<properties>
3737
<!-- Value: replace the text domain used. -->
38-
<property name="text_domain" type="array" value="wordpress-plugin-template"/>
38+
<property name="text_domain" type="array" value="wp-plugin-template"/>
3939
</properties>
4040
</rule>
4141
<rule ref="WordPress.WhiteSpace.ControlStructureSpacing">

src/Install.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace WPT;
4+
5+
class Install
6+
{
7+
protected $plugin_slug;
8+
9+
/**
10+
* Set up
11+
*
12+
* @param string $plugin_slug
13+
*/
14+
public function __construct( string $plugin_slug ) {
15+
$this->plugin_slug = $plugin_slug;
16+
}
17+
18+
/**
19+
* Check if the plugin was just activated
20+
*/
21+
protected function isJustActivated() {
22+
if ( \get_option('wpt_plugin_loaded') == $this->plugin_slug ) {
23+
return true;
24+
}
25+
}
26+
27+
/**
28+
* Remove the option used to check for activation
29+
*/
30+
protected function removeActivationCheck() {
31+
\delete_option('wpt_plugin_loaded');
32+
}
33+
34+
/**
35+
* Add settings defaults
36+
*/
37+
protected function installPresets() {
38+
\update_option('wpt_test_option', 'Yas');
39+
}
40+
41+
/**
42+
* Run it all
43+
*/
44+
public function init() {
45+
if ( !\is_admin() ) {
46+
return;
47+
}
48+
49+
if ( !$this->isJustActivated() ) {
50+
return;
51+
}
52+
53+
$this->removeActivationCheck();
54+
55+
$this->installPresets();
56+
}
57+
}

src/WPT.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace WPT;
4+
5+
class WPT
6+
{
7+
const PLUGIN_NAME = 'WordPress Plugin Template';
8+
const PLUGIN_SLUG = 'wp-plugin-template';
9+
10+
protected $dir_path;
11+
protected $dir_url;
12+
protected $install;
13+
14+
/**
15+
* Set up
16+
*/
17+
public function __construct() {
18+
$this->dir_path = \plugin_dir_path(__FILE__);
19+
$this->dir_url = \plugin_dir_url(__FILE__);
20+
$this->install = new Install( self::PLUGIN_SLUG);
21+
}
22+
23+
/**
24+
* Run it all
25+
*/
26+
public function run() {
27+
$this->install->init();
28+
}
29+
}

wordpress-plugin-template.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,35 @@
44
Description: A simple template for making OOP WordPress plugins.
55
Version: 0.1.0
66
Requires PHP: 7.4
7-
Text Domain: wordpress-plugin-template
7+
Text Domain: wp-plugin-template
88
Author: Seb Kay
99
Author URI: http://www.sebkay.com
1010
*/
11+
12+
defined('ABSPATH') or die('No script kiddies please!');
13+
14+
/**
15+
* Autoloader for dependencies
16+
*/
17+
$wpt_autoload_file = __DIR__ . '/vendor/autoload.php';
18+
19+
if ( is_file($wpt_autoload_file) ) {
20+
require_once $wpt_autoload_file;
21+
}
22+
23+
/**
24+
* Config
25+
*/
26+
$wpt_plugin = new \WPT\WPT();
27+
28+
/**
29+
* Add option when plugin is activated
30+
*/
31+
register_activation_hook(__FILE__, function() use ( $wpt_plugin ) {
32+
add_option('wpt_plugin_loaded', $wpt_plugin::PLUGIN_SLUG);
33+
});
34+
35+
/**
36+
* Run plugin
37+
*/
38+
$wpt_plugin->run();

0 commit comments

Comments
 (0)