cache ) { $stored = get_option( self::OPTION, array() ); $this->cache = is_array( $stored ) ? array_values( $stored ) : array(); // Lite hides text and headings only. $this->cache = array_values( array_filter( $this->cache, function ( $rule ) { return is_array( $rule ) && isset( $rule['type'], $rule['id'], $rule['targets'] ) && 'text' === $rule['type']; } ) ); foreach ( $this->cache as &$rule ) { $rule['status'] = ( isset( $rule['status'] ) && 'paused' === $rule['status'] ) ? 'paused' : 'active'; $rule['scope'] = ( isset( $rule['scope'] ) && 'site' === $rule['scope'] ) ? 'site' : 'page'; } unset( $rule ); } return $this->cache; } /** * Persist the rule list. * * @param array $rules Rules. */ private function persist( array $rules ) { $this->cache = array_values( $rules ); update_option( self::OPTION, $this->cache, true ); /** * Fires after the saved rules changed (created, edited, paused, deleted). */ do_action( 'hidexo_lite_rules_changed' ); } /** * Find a rule by ID. * * @param string $id Rule ID. * @return array|null */ public function get( $id ) { foreach ( $this->all() as $rule ) { if ( isset( $rule['id'] ) && $rule['id'] === $id ) { return $rule; } } return null; } /** * Number of active rules. * * @return int */ public function count_active() { $n = 0; foreach ( $this->all() as $rule ) { if ( isset( $rule['status'] ) && 'active' === $rule['status'] ) { ++$n; } } return $n; } /** * Create a rule. Input must already be validated. * * @param array $data Validated data. * @return array|WP_Error */ public function create( array $data ) { $now = time(); $rule = array_merge( $data, array( 'id' => wp_generate_uuid4(), 'status' => 'active', 'created' => $now, 'updated' => $now, ) ); $rules = $this->all(); array_unshift( $rules, $rule ); $this->persist( $rules ); return $rule; } /** * Update the editable fields: name and where the rule applies. * * @param string $id Rule ID. * @param array $fields Fields. * @return array|WP_Error */ public function update_fields( $id, array $fields ) { return $this->mutate( $id, function ( $rule ) use ( $fields ) { if ( isset( $fields['name'] ) ) { $name = Hidexo_Lite_Rule_Validator::name( $fields['name'] ); if ( '' !== $name ) { $rule['name'] = $name; } } if ( isset( $fields['scope'] ) ) { $rule['scope'] = Hidexo_Lite_Rule_Validator::scope( $fields['scope'] ); } $rule['updated'] = time(); return $rule; } ); } /** * Pause or activate a rule. A paused rule shows the content again. * * @param string $id Rule ID. * @param string $status 'active' or 'paused'. * @return array|WP_Error */ public function set_status( $id, $status ) { $status = ( 'active' === $status ) ? 'active' : 'paused'; return $this->mutate( $id, function ( $rule ) use ( $status ) { $rule['status'] = $status; $rule['updated'] = time(); return $rule; } ); } /** * Store the result of a rule health check. * * @param string $id Rule ID. * @param int $resolved Resolved targets. * @param int $total Total targets. * @return array|WP_Error */ public function set_health( $id, $resolved, $total ) { $total = max( 0, (int) $total ); $resolved = max( 0, min( $total, (int) $resolved ) ); return $this->mutate( $id, function ( $r ) use ( $resolved, $total ) { $r['health'] = array( 'state' => ( $resolved < $total ) ? 'attention' : 'ok', 'resolved' => $resolved, 'total' => $total, 'checked' => time(), ); return $r; } ); } /** * Delete a rule. * * @param string $id Rule ID. * @return bool */ public function delete( $id ) { $rules = $this->all(); $kept = array(); foreach ( $rules as $rule ) { if ( $rule['id'] !== $id ) { $kept[] = $rule; } } if ( count( $kept ) === count( $rules ) ) { return false; } $this->persist( $kept ); return true; } /** * Apply a callback to one rule and save. * * @param string $id Rule ID. * @param callable $cb Callback receiving and returning the rule. * @return array|WP_Error */ private function mutate( $id, $cb ) { $rules = $this->all(); foreach ( $rules as $i => $rule ) { if ( $rule['id'] === $id ) { $rules[ $i ] = call_user_func( $cb, $rule ); $this->persist( $rules ); return $rules[ $i ]; } } return new WP_Error( 'hidexo_not_found', __( 'Rule not found.', 'hidexo-lite' ), array( 'status' => 404 ) ); } /** * Active rules that apply to a request path. * * @param string $path Normalized path. * @return array */ public function active_for_path( $path ) { $out = array(); foreach ( $this->all() as $rule ) { if ( 'active' !== $rule['status'] ) { continue; } if ( 'site' === $rule['scope'] || $rule['page_path'] === $path ) { $out[] = $rule; } } return $out; } /** * Number of hidden elements across active rules. * * @return int */ public function hidden_element_count() { $n = 0; foreach ( $this->all() as $rule ) { if ( 'active' === $rule['status'] ) { $n += count( $rule['targets'] ); } } return $n; } /** * Shape a rule for the dashboard (targets are omitted). * * @param array $rule Rule. * @return array */ public function public_view( array $rule ) { return array( 'id' => $rule['id'], 'name' => $rule['name'], 'type' => $rule['type'], 'status' => $rule['status'], 'scope' => $rule['scope'], 'page_path' => $rule['page_path'], 'page_title' => isset( $rule['page_title'] ) ? $rule['page_title'] : '', 'preview' => isset( $rule['preview'] ) ? $rule['preview'] : '', 'targetCount' => count( $rule['targets'] ), 'health' => isset( $rule['health'] ) ? $rule['health'] : array( 'state' => 'unchecked' ), 'created' => (int) $rule['created'], 'updated' => (int) $rule['updated'], ); } /** * Normalize a URL or path to a comparable site-relative path. * * @param string $url URL or path. * @return string */ public static function normalize_path( $url ) { $path = wp_parse_url( $url, PHP_URL_PATH ); $path = is_string( $path ) ? rawurldecode( $path ) : '/'; $base = wp_parse_url( home_url(), PHP_URL_PATH ); $base = is_string( $base ) ? untrailingslashit( $base ) : ''; if ( '' !== $base && 0 === strpos( $path, $base . '/' ) ) { $path = substr( $path, strlen( $base ) ); } elseif ( $base === $path ) { $path = '/'; } $path = '/' . trim( $path, '/' ); return strtolower( $path ); } /** * Normalized path of the current front-end request. * * @return string */ public static function current_path() { $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/'; return self::normalize_path( $uri ); } }