prefix}nodebook_{module id}_{name} * - options: nodebook_mod_{module id}_{name} * - scheduled tasks: nodebook_mod_{module id}_{name} * * @package Nodebook */ defined( 'ABSPATH' ) || exit; abstract class Nodebook_Module_Base implements Nodebook_Module { /** @var string Module ID (lowercase letters, numbers and dashes). */ protected $id; public function __construct( $id ) { $this->id = (string) $id; } public function id() { return $this->id; } public function boot() {} public function install() {} public function deactivate() { foreach ( $this->cron_hooks() as $hook ) { wp_unschedule_hook( $hook ); } } /** Drops the module's tables, options and scheduled tasks. Override to remove more, then call parent::uninstall(). */ public function uninstall() { global $wpdb; $this->deactivate(); foreach ( Nodebook_Modules::tables( $this->id ) as $table ) { $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- removing the module's own tables on request. } $like = $wpdb->esc_like( self::slug( 'nodebook_mod_' . $this->id . '_' ) ) . '%'; foreach ( (array) $wpdb->get_col( $wpdb->prepare( 'SELECT option_name FROM %i WHERE option_name LIKE %s', $wpdb->options, $like ) ) as $name ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- finding the module's own options. delete_option( $name ); } } /** Scheduled task hooks the module uses (cleared on deactivate). By default: every hook starting with nodebook_mod_{id}_. */ protected function cron_hooks() { $prefix = self::slug( 'nodebook_mod_' . $this->id . '_' ); $hooks = array(); foreach ( (array) _get_cron_array() as $events ) { foreach ( array_keys( (array) $events ) as $hook ) { if ( 0 === strpos( (string) $hook, $prefix ) ) { $hooks[ $hook ] = true; } } } return array_keys( $hooks ); } /** Full table name for one of the module's tables. */ public function table( $name ) { global $wpdb; return $wpdb->prefix . self::slug( 'nodebook_' . $this->id . '_' . $name ); } /** * Creates or upgrades one of the module's tables with dbDelta, and records it so Nodebook can drop it later. * * @param string $name Short name, e.g. "deals". * @param string $columns Everything between the brackets of CREATE TABLE (dbDelta format: two spaces before PRIMARY KEY). * @return string Full table name. */ protected function create_table( $name, $columns ) { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = $this->table( $name ); dbDelta( "CREATE TABLE {$table} (\n" . trim( $columns ) . "\n) " . $wpdb->get_charset_collate() . ';' ); Nodebook_Modules::track_table( $this->id, $table ); return $table; } /** Name of a scheduled task hook that Nodebook clears for you. */ protected function cron_hook( $name ) { return self::slug( 'nodebook_mod_' . $this->id . '_' . $name ); } public function get_option( $name, $fallback = false ) { return get_option( self::slug( 'nodebook_mod_' . $this->id . '_' . $name ), $fallback ); } public function update_option( $name, $value ) { return update_option( self::slug( 'nodebook_mod_' . $this->id . '_' . $name ), $value, false ); } /** Dashes are fine in IDs but not in table names. */ private static function slug( $s ) { return str_replace( '-', '_', strtolower( $s ) ); } }