summaryrefslogtreecommitdiffstats
path: root/includes/class-cepis-woo-admin.php
blob: efb1959492e046f9c2a497c4b86ef51b60e1a6fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
 * Admin-side wiring.
 *
 * @package CepisWooCommerce
 */

declare(strict_types=1);

defined( 'ABSPATH' ) || exit;

/**
 * Puts the settings screen where a store owner will look for it, and links to
 * it from the plugins list.
 */
final class Cepis_Woo_Admin {

	/**
	 * Hook everything the admin needs.
	 */
	public static function init(): void {
		add_filter( 'woocommerce_get_settings_pages', array( self::class, 'register_settings_page' ) );
		add_filter( 'plugin_action_links_' . plugin_basename( CEPIS_WOO_FILE ), array( self::class, 'action_links' ) );
	}

	/**
	 * Add the settings tab to WooCommerce's own settings screen.
	 *
	 * The class is required here rather than at load time because it extends
	 * `WC_Settings_Page`, which does not exist until WooCommerce has booted its
	 * admin.
	 *
	 * @param array<int, mixed> $pages Settings pages WooCommerce has collected.
	 *
	 * @return array<int, mixed>
	 */
	public static function register_settings_page( array $pages ): array {
		if ( ! class_exists( 'WC_Settings_Page' ) ) {
			return $pages;
		}

		require_once CEPIS_WOO_DIR . 'includes/class-cepis-woo-settings.php';

		$pages[] = new Cepis_Woo_Settings();

		return $pages;
	}

	/**
	 * Add a "Settings" link to the plugin's row.
	 *
	 * @param array<int|string, string> $links Existing action links.
	 *
	 * @return array<int|string, string>
	 */
	public static function action_links( array $links ): array {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			return $links;
		}

		$url = admin_url( 'admin.php?page=wc-settings&tab=cepis' );

		array_unshift(
			$links,
			sprintf(
				'<a href="%s">%s</a>',
				esc_url( $url ),
				esc_html__( 'Settings', 'cepis-woocommerce' )
			)
		);

		return $links;
	}
}