plugin_file = $plugin_file; } /** * Whether MainWP is present (its dashboard filters are registered). * * @return bool */ public function mainwp_available(): bool { return has_filter( 'mainwp_extension_enabled_check' ) && has_filter( 'mainwp_fetchurlauthed' ); } /** * Obtain this extension's MainWP auth key. * * MainWP returns array( 'key' => md5( plugin_file . '-SNNonceAdder' ) ). * * @return string Empty string if MainWP is unavailable or returns no key. */ public function get_key(): string { $enabled = apply_filters( 'mainwp_extension_enabled_check', $this->plugin_file ); if ( is_array( $enabled ) && ! empty( $enabled['key'] ) ) { return (string) $enabled['key']; } return ''; } /** * List managed sites visible to this extension. * * @return array> Normalized site rows (id, name, url). */ public function get_sites(): array { $key = $this->get_key(); if ( '' === $key ) { return array(); } $sites = apply_filters( 'mainwp_getsites', $this->plugin_file, $key, null ); if ( ! is_array( $sites ) ) { return array(); } $normalized = array(); foreach ( $sites as $site ) { $site = (array) $site; if ( empty( $site['id'] ) ) { continue; } $normalized[] = array( 'id' => (int) $site['id'], 'name' => isset( $site['name'] ) ? (string) $site['name'] : '', 'url' => isset( $site['url'] ) ? (string) $site['url'] : '', ); } return $normalized; } /** * Call a managed site's Connector with an AdSanity action. * * @param int $site_id The MainWP site id. * @param string $action The adsanity_action to dispatch (ping/stats/inventory/meta/deploy). * @param array $params Optional extra params merged into the POST payload. * @return array{ok: bool, error?: string, data?: array} Normalized result. */ public function call_child( int $site_id, string $action, array $params = array() ): array { $key = $this->get_key(); if ( '' === $key ) { return array( 'ok' => false, 'error' => 'mainwp_unavailable', ); } $payload = array_merge( array( 'adsanity_action' => $action ), $params ); $response = apply_filters( 'mainwp_fetchurlauthed', $this->plugin_file, $key, $site_id, 'extra_execution', $payload ); if ( false === $response || ! is_array( $response ) ) { return array( 'ok' => false, 'error' => 'no_response', ); } if ( isset( $response['error'] ) ) { return array( 'ok' => false, 'error' => (string) $response['error'], ); } if ( ! isset( $response['adsanity'] ) || ! is_array( $response['adsanity'] ) ) { // Connector did not answer (missing/old) — response carried no AdSanity key. return array( 'ok' => false, 'error' => 'connector_absent', ); } return array( 'ok' => true, 'data' => $response['adsanity'], ); } /** * Connectivity probe against a single site. * * @param int $site_id The MainWP site id. * @return array{ok: bool, error?: string, data?: array} */ public function ping_site( int $site_id ): array { return $this->call_child( $site_id, 'ping' ); } /** * Ping every managed site and collect per-site results. * * @return array> One row per site (id, name, url, result). */ public function ping_all_sites(): array { $results = array(); foreach ( $this->get_sites() as $site ) { $results[] = array( 'id' => $site['id'], 'name' => $site['name'], 'url' => $site['url'], 'result' => $this->ping_site( $site['id'] ), ); } return $results; } }