'array',
'sanitize_callback' => array( $this->sanitizer, 'sanitize' ),
'default' => Options::defaults(),
)
);
}
/**
* Loads the style and script on this page.
*
* @param string $hook Admin page hook.
* @return void
*/
public function enqueue( string $hook ): void {
if ( 'toplevel_page_' . self::SLUG !== $hook ) {
return;
}
Assets::style();
wp_enqueue_script( 'agentwelcome-admin', AGENTWELCOME_URL . 'assets/js/admin.js', array(), Assets::version( 'assets/js/admin.js' ), true );
}
/**
* Adds a Settings link to the plugin's row on the Plugins screen.
*
* @param string[] $links Existing action links.
* @return string[] The links, with Settings first.
*/
public function action_links( array $links ): array {
array_unshift( $links, '' . esc_html__( 'Settings', 'agentwelcome' ) . '' );
return $links;
}
/**
* Address of the page.
*
* @return string Admin address.
*/
public function url(): string {
return admin_url( 'admin.php?page=' . self::SLUG );
}
/**
* Prints the page.
*
* @return void
*/
public function render(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
View::render(
'page',
array(
'tabs' => array(
'content' => __( 'Content', 'agentwelcome' ),
'rendering' => __( 'Rendering', 'agentwelcome' ),
'environment' => __( 'Environment', 'agentwelcome' ),
),
'settings_url' => $this->url(),
'notices' => $this->notices->data(),
'after_notices' => 'settings_errors',
'content' => array( $this, 'render_form' ),
)
);
}
/**
* Prints the settings form inside the page.
*
* @return void
*/
public function render_form(): void {
View::render(
'settings-form',
array(
'group' => self::GROUP,
'name' => Options::KEY,
'options' => $this->options,
'environment' => $this->environment,
'types' => $this->types( true ),
'term_groups' => $this->terms->groups(),
'term_limit' => TermList::LIMIT,
'home_urls' => array(
Urls::HOME_SHORT => home_url( '/' . Urls::HOME_SHORT ),
Urls::HOME_LONG => home_url( '/' . Urls::HOME_LONG ),
),
'home_collision' => $this->urls->index_collision(),
'excluded_terms' => array_map( 'absint', (array) $this->options->get( 'excluded_terms', array() ) ),
'common_content' => self::COMMON_CONTENT,
'common_excludes' => self::COMMON_EXCLUDES,
'status' => $this->status(),
)
);
}
/**
* Values for the status card.
*
* @return array Values by name.
*/
private function status(): array {
global $wp_version;
$reason = $this->environment->reason();
return array(
'inactive' => null === $reason ? null : $this->environment->describe( $reason ),
'pretty' => Urls::has_pretty_permalinks(),
'types' => $this->types( false ),
'cache' => $this->coverage->summary(),
'running' => $this->batch->is_running(),
'cron_disabled' => $this->environment->cron_disabled(),
'verify_url' => VerifyPage::url(),
'home_file' => $this->urls->home_file(),
'system' => array(
__( 'WordPress', 'agentwelcome' ) => (string) $wp_version,
__( 'PHP', 'agentwelcome' ) => PHP_VERSION,
__( 'AgentWelcome', 'agentwelcome' ) => AGENTWELCOME_VERSION,
__( 'Environment', 'agentwelcome' ) => $this->environment->type(),
),
);
}
/**
* Post types for the settings form.
*
* @param bool $all Every available type with its exclusion state, or only the enabled ones.
* @return array One entry per type.
*/
private function types( bool $all ): array {
$types = array();
foreach ( $all ? $this->post_types->available() : $this->post_types->enabled() as $type ) {
$object = get_post_type_object( $type );
$types[] = array(
'slug' => $type,
'label' => $object ? $object->labels->name : $type,
'published' => $this->published_label( $type ),
'excluded' => in_array( $type, $this->post_types->excluded(), true ),
);
}
return $types;
}
/**
* Text that says how many pages of a type are published.
*
* @param string $type Post type.
* @return string Text such as "12 published".
*/
private function published_label( string $type ): string {
$counts = wp_count_posts( $type );
$total = is_object( $counts ) && isset( $counts->publish ) ? (int) $counts->publish : 0;
/* translators: %s: number of published posts. */
return sprintf( _n( '%s published', '%s published', $total, 'agentwelcome' ), number_format_i18n( $total ) );
}
}