is_page_active() ) { wp_enqueue_script( 'clipboard' ); wp_enqueue_style( 'defender' ); wp_enqueue_script( 'wpmudev-sui' ); } } /** * This too check if the current page is active, so we can queue right assets. * * @return bool */ public function is_page_active() { $current = HTTP::get( 'page' ); return $current === $this->slug; } /** * Quick handler to check nonce. * * @param string $intention Should give context to what is taking place and be the same when nonce was created. * @param string $method Current request method. * * @return bool */ protected function verify_nonce( $intention, $method = 'get' ) { $nonce = 'get' === $method ? HTTP::get( '_def_nonce' ) : HTTP::post( '_def_nonce' ); if ( ! wp_verify_nonce( $nonce, $intention ) ) { return false; } return true; } /** * Bind for submit data to DEV. * * @return void */ public function queue_to_sync_with_hub() { if ( ! wp_next_scheduled( 'defender_hub_sync' ) ) { wp_schedule_single_event( time(), 'defender_hub_sync' ); } } /** * Read through this class and generate a list of intention method, register it with the central. * The methods that have annotation @defender_method will be registered automatically. */ public function register_routes() { foreach ( $this->get_methods() as $method ) { $doc_block = $method->getDocComment(); if ( stristr( $doc_block, '@defender_route' ) ) { if ( 'register_routes' === $method->getName() ) { continue; } $is_public = stristr( $doc_block, '@is_public' ); $is_redirect = stristr( $doc_block, '@defender_redirect' ); wd_central()->add_route( $method->getName(), static::class, ! $is_public, $is_redirect ); } } } /** * Return all methods from current class. * * @return ReflectionMethod[] */ private function get_methods() { $class = new ReflectionClass( static::class ); return $class->getMethods( ReflectionMethod::IS_PUBLIC ); } /** * Dump the routes and nonces. * * @return array[] */ public function dump_routes_and_nonces() { $nonces = array(); $routes = array(); foreach ( $this->get_methods() as $method ) { $doc_block = $method->getDocComment(); if ( stristr( $doc_block, '@defender_route' ) ) { if ( 'register_routes' === $method->getName() ) { continue; } $nonces[ $method->getName() ] = wd_central()->get_nonce( $method->getName(), static::class ); $routes[ $method->getName() ] = wd_central()->get_route( $method->getName(), static::class ); } } return array( 'routes' => $routes, 'nonces' => $nonces, ); } /** * Check if DEFENDER_DEBUG is enabled for the route. * * @param string $route Route to check. * * @return string|array */ public function check_route( string $route ) { return defined( 'DEFENDER_DEBUG' ) && true === constant( 'DEFENDER_DEBUG' ) ? wp_slash( $route ) : $route; } }