Last active
March 5, 2021 07:48
-
-
Save apermo/3caf33c690594bc98ab261df0590d130 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Contains Class Settings_Abstract | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
header( 'HTTP/1.0 404 Not Found' ); | |
exit( 'You shall not pass' ); | |
} | |
/** | |
* Class Settings_Abstract | |
*/ | |
abstract class Settings_Abstract { | |
/** | |
* Settings Version, needs to be incremental integer (either counting up, or using some timestamp) | |
* | |
* @var int | |
*/ | |
protected $settings_version; | |
/** | |
* The basename(s) of the corresponding plugin | |
* | |
* @var string|array | |
*/ | |
protected $plugin_basename; | |
/** | |
* Settings Key for this set of settings | |
* | |
* @var string | |
*/ | |
protected $settings_version_key; | |
/** | |
* Action Key, will be preset automatically | |
* | |
* @var string | |
*/ | |
protected $action_key; | |
/** | |
* Registered Settings | |
* | |
* @var array | |
*/ | |
private $registered_settings = []; | |
public function __construct() { | |
$this->action_key = $this->action_key ?? strtolower( get_class( $this ) ) . '_updates'; | |
$this->settings_version_key = $this->settings_version_key ?? strtolower( get_class( $this ) ) . '_version'; | |
$this->init_register(); | |
$this->update_settings(); | |
} | |
abstract protected function init_register() : void; | |
public function update_settings() : void { | |
if ( ! is_admin() ) { | |
return; | |
} | |
if ( ! current_user_can( 'manage_options' ) ) { | |
return; | |
} | |
if ( $this->plugin_basename && ! $this->is_plugin_active( (array) $this->plugin_basename ) ) { | |
return; | |
} | |
$last_update_version = get_option( $this->settings_version_key ); | |
if ( $this->settings_version <= $last_update_version ) { | |
return; | |
} | |
foreach ( $this->registered_settings as $settings ) { | |
if ( $settings['version'] <= $last_update_version ) { | |
// Already done this, so skip this. | |
continue; | |
} | |
add_action( $this->action_key, $settings['callable'], $settings['version'] ); | |
} | |
do_action( $this->action_key ); | |
update_option( $this->settings_version_key, $this->settings_version ); | |
} | |
private function is_plugin_active( $basenames ) : bool { | |
foreach ( $basenames as $basename ) { | |
if ( ! function_exists( 'is_plugin_active' ) ) { | |
require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); | |
} | |
if ( is_plugin_active( $basename ) ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* @param int $version | |
* @param callable $callable | |
*/ | |
protected function register_setting( $version, $callable ) : void { | |
$this->registered_settings[] = [ | |
'version' => $version, | |
'callable' => $callable, | |
]; | |
} | |
/** | |
* Preset Option | |
* | |
* @param string $key Options key. | |
* @param callable $callable Callback | |
* @param int $priority Filter Priority. | |
* @param int $args Number of arguments presented to the callback. | |
*/ | |
protected function preset_option( $key, $callable, $priority = 99, $args = 1 ) : void { | |
add_filter( "pre_option_{$key}", $callable, $priority, $args ); | |
} | |
/** | |
* Filter Option | |
* | |
* @param string $key Options key. | |
* @param callable $callable Callback | |
* @param int $priority Filter Priority. | |
* @param int $args Number of arguments presented to the callback. | |
*/ | |
protected function filter_option( $key, $callable, $priority = 99, $args = 1 ) : void { | |
add_filter( "option_{$key}", $callable, $priority, $args ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example use | |
* Contains Class Settings_WordPress | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
header( 'HTTP/1.0 404 Not Found' ); | |
exit( 'You shall not pass' ); | |
} | |
class Settings_WordPress extends Settings_Abstract { | |
/** | |
* Settings Version, needs to be incremental integer (either counting up, or using some timestamp) | |
* | |
* @var int | |
*/ | |
protected $settings_version = 6; | |
/** | |
* Init all the registered settings and presets | |
*/ | |
protected function init_register() : void { | |
$this->preset_option( 'blog_public', [ $this, 'blog_public' ] ); | |
$this->register_setting( 3, [ $this, 'register_default_user' ] ); | |
$this->register_setting( 5, [ $this, 'permalink_structure' ] ); | |
$this->register_setting( 6, [ $this, 'set_autoincrement' ] ); | |
} | |
/** | |
* Preset Callback for: get_option( 'blog_public' ); | |
* | |
* Automatically block search engines when not in production mode | |
* | |
* @param mixed $value the Value from the filter. | |
* | |
* @return mixed | |
*/ | |
public function blog_public( $value ) { | |
if ( defined( 'PRODUCTION_MODE' ) && PRODUCTION_MODE ) { | |
return $value; | |
} | |
return 0; | |
} | |
/** | |
* Defines: wp_options: permalink_structure | |
*/ | |
public function permalink_structure() : void { | |
update_option( 'permalink_structure', '/%category%/%postname%-%post_id%/' ); | |
flush_rewrite_rules(); | |
} | |
/** | |
* Registers a default user. | |
*/ | |
public function register_default_user() : void { | |
$result = wp_insert_user( | |
[ | |
'user_login' => 'Default', | |
'user_pass' => wp_generate_password( 32 ), | |
'user_nicename' => 'Default', | |
'display_name' => 'Default', | |
'role' => 'contributor', | |
] | |
); | |
if ( ! $result instanceof WP_Error ) { | |
update_user_meta( $result, '_my_custom_meta', true ); | |
} | |
} | |
/** | |
* Set the Auto Increment to 1 000 000 000 or above to make sure post ids and web story ids are distinct. | |
*/ | |
public function set_autoincrement(): void { | |
global $wpdb; | |
$wpdb->query( "ALTER TABLE $wpdb->posts AUTO_INCREMENT = 1000000000;" ); | |
} | |
} | |
new Settings_WordPress(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment