create|update|delete|file|local|info|warn, 'text' => string]. */ abstract public function preview( array $opts ); /** * Apply. * * @param array $opts Options. * @return array|WP_Error ['messages' => string[]]. */ abstract public function apply( array $opts ); /** * Undo everything apply() did. * * @return array|WP_Error */ abstract public function rollback(); /** * Requires a Pro license? * * @return bool */ public function pro() { return false; } /** * Whether the module can be used on this site/plan. True or a reason string. * * @return true|string */ public function available() { return true; } /** * Option definitions: key => [type, label, default, help, choices]. * * @return array */ public function option_defs() { return array(); } /** * Stored options merged with defaults. * * @return array */ public function options() { $state = Store::module( $this->id() ); $stored = isset( $state['options'] ) && is_array( $state['options'] ) ? $state['options'] : array(); $opts = array(); foreach ( $this->option_defs() as $key => $def ) { $opts[ $key ] = array_key_exists( $key, $stored ) ? $stored[ $key ] : $def['default']; } return $opts; } /** * Sanitize options coming from the browser against option_defs(). * * @param array $raw Raw values. * @return array */ public function sanitize_options( array $raw ) { $out = $this->options(); foreach ( $this->option_defs() as $key => $def ) { if ( ! array_key_exists( $key, $raw ) ) { if ( 'bool' === $def['type'] ) { $out[ $key ] = false; } continue; } $value = $raw[ $key ]; switch ( $def['type'] ) { case 'bool': $out[ $key ] = in_array( $value, array( true, 1, '1', 'true', 'on', 'yes' ), true ); break; case 'int': $int = (int) $value; if ( isset( $def['min'] ) ) { $int = max( (int) $def['min'], $int ); } if ( isset( $def['max'] ) ) { $int = min( (int) $def['max'], $int ); } $out[ $key ] = $int; break; case 'select': $value = sanitize_key( (string) $value ); $out[ $key ] = isset( $def['choices'][ $value ] ) ? $value : $def['default']; break; case 'lines': $lines = preg_split( '/[\r\n,]+/', (string) $value ); $out[ $key ] = implode( "\n", array_filter( array_map( 'sanitize_text_field', $lines ) ) ); break; default: $out[ $key ] = sanitize_text_field( (string) $value ); } } return $out; } /** * Is the module switched on? * * @return bool */ public function enabled() { $state = Store::module( $this->id() ); return ! empty( $state['enabled'] ); } /** * Runtime hooks (front end / admin). Called on every request when connected. */ public function register() {} /** * Live verification. Returns checks and stores them. * * @return array List of ['label' => string, 'ok' => bool|null, 'detail' => string]. */ public function verify() { return array(); } /** * Extra actions for this module (e.g. "purge_all", "scan"). * * @param string $action Action name. * @param array $args Arguments. * @return array|WP_Error */ public function action( $action, array $args ) { return new WP_Error( 'fehu_ap_unknown_action', __( 'Unknown action.', 'zonepilot' ) ); } /** * Seconds an operation may need because it waits for Cloudflare and checks the result live. * 0 = a normal, short request (the PHP time limit is left alone). * * @param string $operation "apply" or an action name. * @return int */ public function time_needed( $operation ) { return 0; } /** * Data for the dashboard card. * * @return array */ public function card() { $state = Store::module( $this->id() ); return array( 'id' => $this->id(), 'title' => $this->title(), 'summary' => $this->summary(), 'pro' => $this->pro(), 'locked' => $this->pro() && ! License::is_pro(), 'available' => $this->available(), 'enabled' => $this->enabled(), 'options' => $this->options(), 'defs' => $this->option_defs(), 'applied' => isset( $state['applied_at'] ) ? (int) $state['applied_at'] : 0, 'checks' => isset( $state['checks'] ) ? $state['checks'] : array(), 'checked' => isset( $state['checked_at'] ) ? (int) $state['checked_at'] : 0, 'extra' => $this->card_extra(), ); } /** * Module specific card data. * * @return array */ public function card_extra() { return array(); } /** * Mark the module applied with options. * * @param array $opts Options. * @param array $extra Extra state. */ protected function mark_applied( array $opts, array $extra = array() ) { Store::update_module( $this->id(), array_merge( array( 'enabled' => true, 'options' => $opts, 'applied_at' => time(), ), $extra ) ); } /** * Mark the module rolled back. */ protected function mark_off() { $state = Store::module( $this->id() ); Store::set_module( $this->id(), array( 'enabled' => false, 'options' => isset( $state['options'] ) ? $state['options'] : array(), ) ); } /** * Store verification results. * * @param array $checks Checks. * @return array */ protected function store_checks( array $checks ) { Store::update_module( $this->id(), array( 'checks' => $checks, 'checked_at' => time(), ) ); return $checks; } /** * Helper to build a check row. * * @param string $label Label. * @param bool|null $ok True ok, false fail, null warning/unknown. * @param string $detail Detail. * @return array */ protected static function check( $label, $ok, $detail = '' ) { return array( 'label' => $label, 'ok' => $ok, 'detail' => $detail, ); } }