cf = $cf ? $cf : Cloudflare::stored(); $this->zone = null === $zone ? Site::zone_id() : $zone; } /** * Find a rule by ref inside a ruleset. * * @param array $ruleset Ruleset. * @param string $ref Ref. * @return array|null */ private static function find( $ruleset, $ref ) { foreach ( (array) ( isset( $ruleset['rules'] ) ? $ruleset['rules'] : array() ) as $rule ) { if ( isset( $rule['ref'] ) && $rule['ref'] === $ref ) { return $rule; } } return null; } /** * Create or update one of our rules. * * @param string $phase Phase. * @param string $name Short name (becomes part of the ref). * @param array $rule Rule without ref (expression, action, action_parameters, description...). * @return array|WP_Error {ruleset_id, rule_id, ref, created} */ public function ensure( $phase, $name, array $rule ) { $ref = Site::ref( $name ); $rule['ref'] = $ref; if ( ! isset( $rule['enabled'] ) ) { $rule['enabled'] = true; } $entry = $this->cf->entrypoint( $this->zone, $phase ); if ( is_wp_error( $entry ) ) { return $entry; } if ( null === $entry ) { $created = $this->cf->create_entrypoint( $this->zone, $phase, array( $rule ) ); if ( is_wp_error( $created ) ) { return $created; } $found = self::find( $created, $ref ); return array( 'ruleset_id' => $created['id'], 'rule_id' => $found ? $found['id'] : '', 'ref' => $ref, 'created' => true, ); } $existing = self::find( $entry, $ref ); if ( $existing ) { $res = $this->cf->update_rule( $this->zone, $entry['id'], $existing['id'], $rule ); } else { $res = $this->cf->add_rule( $this->zone, $entry['id'], $rule ); } if ( is_wp_error( $res ) ) { return $res; } $found = self::find( $res, $ref ); return array( 'ruleset_id' => $entry['id'], 'rule_id' => $found ? $found['id'] : '', 'ref' => $ref, 'created' => ! $existing, ); } /** * Delete one of our rules (no-op when it is gone already). * * @param string $phase Phase. * @param string $name Short name. * @return true|WP_Error */ public function remove( $phase, $name ) { $entry = $this->cf->entrypoint( $this->zone, $phase ); if ( is_wp_error( $entry ) ) { return $entry; } if ( null === $entry ) { return true; } $existing = self::find( $entry, Site::ref( $name ) ); if ( ! $existing ) { return true; } $res = $this->cf->delete_rule( $this->zone, $entry['id'], $existing['id'] ); return is_wp_error( $res ) ? $res : true; } /** * Our rule (or null). * * @param string $phase Phase. * @param string $name Short name. * @return array|null|WP_Error */ public function get( $phase, $name ) { $entry = $this->cf->entrypoint( $this->zone, $phase ); if ( is_wp_error( $entry ) ) { return $entry; } return null === $entry ? null : self::find( $entry, Site::ref( $name ) ); } /** * Count of rules in a phase (to warn about plan limits). * * @param string $phase Phase. * @return int|WP_Error */ public function count( $phase ) { $entry = $this->cf->entrypoint( $this->zone, $phase ); if ( is_wp_error( $entry ) ) { return $entry; } return null === $entry ? 0 : count( (array) ( isset( $entry['rules'] ) ? $entry['rules'] : array() ) ); } }