*/ public function all() { $value = get_option( self::OPTION, array() ); return is_array( $value ) ? $value : array(); } /** * Perform the documented operation. * * @param int $form_id Form ID. * @return array */ public function for_form( $form_id ) { $all = $this->all(); $forms = isset( $all['forms'] ) && is_array( $all['forms'] ) ? $all['forms'] : array(); $setting = isset( $forms[ absint( $form_id ) ] ) && is_array( $forms[ absint( $form_id ) ] ) ? $forms[ absint( $form_id ) ] : array(); return array( 'enabled' => ! empty( $setting['enabled'] ), 'tool_name' => ToolNaming::resolve( $form_id, isset( $setting['tool_name'] ) ? (string) $setting['tool_name'] : '' ), ); } /** * Perform the documented operation. * * @param int $form_id Form ID. * @param bool $enabled Whether exposure is enabled. * @param string $tool_name Optional custom name. * @return bool */ public function save_form( $form_id, $enabled, $tool_name = '' ) { $all = $this->all(); $all['forms'] = isset( $all['forms'] ) && is_array( $all['forms'] ) ? $all['forms'] : array(); $form_id = absint( $form_id ); $all['forms'][ $form_id ] = array( 'enabled' => (bool) $enabled, 'tool_name' => ToolNaming::resolve( $form_id, sanitize_text_field( (string) $tool_name ) ), ); return update_option( self::OPTION, $all, false ); } /** * Perform the documented operation. * * @param array $forms Discovered forms. * @return array> */ public function exposed_forms( array $forms ) { $exposed = array(); $candidates = array(); $counts = array(); foreach ( $forms as $form ) { if ( $form->is_compatible() ) { $setting = $this->for_form( $form->id() ); if ( ! empty( $setting['enabled'] ) ) { $candidates[] = array( 'form' => $form, 'settings' => $setting, ); $tool_name = $setting['tool_name']; $counts[ $tool_name ] = isset( $counts[ $tool_name ] ) ? $counts[ $tool_name ] + 1 : 1; } } } foreach ( $candidates as $candidate ) { $tool_name = $candidate['settings']['tool_name']; if ( 1 === $counts[ $tool_name ] ) { $exposed[] = $candidate; } } return $exposed; } /** * Return enabled compatible forms whose resolved tool names collide. * * @param array $forms Discovered forms. * @return array Map of form ID to duplicated tool name. */ public function tool_name_conflicts( array $forms ) { $enabled = array(); $counts = array(); foreach ( $forms as $form ) { if ( ! $form->is_compatible() ) { continue; } $setting = $this->for_form( $form->id() ); if ( empty( $setting['enabled'] ) ) { continue; } $tool_name = $setting['tool_name']; $enabled[ $form->id() ] = $tool_name; $counts[ $tool_name ] = isset( $counts[ $tool_name ] ) ? $counts[ $tool_name ] + 1 : 1; } $conflicts = array(); foreach ( $enabled as $form_id => $tool_name ) { if ( 1 < $counts[ $tool_name ] ) { $conflicts[ $form_id ] = $tool_name; } } return $conflicts; } }