MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 8.0.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/.trash/rj-google-signin/src/Modules/Shortcode.php
<?php
/**
 * Shortcode Class.
 *
 * @package RjoshiWebdev\GoogleSignIn
 * @since 1.0.0
 */

declare(strict_types=1);

namespace RjoshiWebdev\GoogleSignIn\Modules;

use RjoshiWebdev\GoogleSignIn\Interfaces\Module as ModuleInterface;
use RjoshiWebdev\GoogleSignIn\Utils\Helper;
use RjoshiWebdev\GoogleSignIn\Utils\GoogleClient;
use function RjoshiWebdev\GoogleSignIn\plugin;

/**
 * Class Shortcode
 *
 * @package RjoshiWebdev\GoogleSignIn
 */
class Shortcode implements ModuleInterface {

	/**
	 * Shortcode tag.
	 *
	 * @var string
	 */
	const TAG = 'google_signin';

	/**
	 * Redirect URL.
	 *
	 * @var string
	 */
	public $redirect_uri;

	/**
	 * Google client instance.
	 *
	 * @var GoogleClient
	 */
	private $gh_client;

	/**
	 * Assets object.
	 *
	 * @var Assets
	 */
	private $assets;

	/**
	 * Shortcode constructor.
	 *
	 * @param GoogleClient $client GH Client object.
	 * @param Assets       $assets Assets object.
	 */
	public function __construct( GoogleClient $client, Assets $assets ) {
		$this->gh_client = $client;
		$this->assets    = $assets;
	}

	/**
	 * Module name.
	 *
	 * @return string
	 */
	public function name(): string {
		return 'shortcode';
	}

	/**
	 * Initialization actions.
	 */
	public function init(): void {
		add_shortcode( self::TAG, [ $this, 'callback' ] );
		add_filter( 'do_shortcode_tag', [ $this, 'scan_shortcode' ], 10, 3 );
	}

	/**
	 * Callback function for shortcode rendering.
	 *
	 * @param array $attrs Shortcode attributes.
	 *
	 * @return string
	 */
	public function callback( $attrs = [] ): string {
		$attrs = shortcode_atts(
			[
				'button_text'   => __( 'Rj Google SignIn', 'rj-google-signin' ),
				'force_display' => 'no',
				'redirect_to'   => get_permalink(),
			],
			$attrs,
			self::TAG
		);

		if ( ! $this->should_display( $attrs ) ) {
			return '';
		}

		$this->redirect_uri = $attrs['redirect_to'];

		add_filter( 'RjoshiWebdev.google_redirect_url', [ $this, 'redirect_url' ] );
		add_filter( 'RjoshiWebdev.google_signin_state', [ $this, 'state_redirect' ] );

		$attrs['login_url'] = $this->gh_client->authorization_url();

		remove_filter( 'RjoshiWebdev.google_signin_state', [ $this, 'state_redirect' ] );
		remove_filter( 'RjoshiWebdev.google_redirect_url', [ $this, 'redirect_url' ] );
		$template = trailingslashit( plugin()->template_dir ) . 'google-login-button.php';

		return Helper::render_template( $template, $attrs, false );
	}

	/**
	 * Check if the current single post or page contains
	 * shortcode. If it does, enqueue the relevant style.
	 *
	 * @param string       $output Shortcode output.
	 * @param string       $tag Shortcode tag being processed.
	 * @param array|string $attrs Shortcode attributes.
	 *
	 * @return string
	 */
	public function scan_shortcode( string $output, string $tag, $attrs ): string {
		if ( ( ! is_single() && ! is_page() ) || self::TAG !== $tag || ! $this->should_display( (array) $attrs ) ) {
			return $output;
		}

		$this->assets->enqueue_login_styles();

		return $output;
	}


	/**
	 * Filter redirect URL as per shortcode param.
	 *
	 * @param string $url SignIn URL.
	 *
	 * @return string
	 */
	public function redirect_url( string $url ): string {

		return remove_query_arg( 'redirect_to', $url );
	}

	/**
	 * Add redirect_to location in state.
	 *
	 * @param array $state State data.
	 *
	 * @return array
	 */
	public function state_redirect( array $state ): array {
		if ( is_null( $this->redirect_uri ) ) {
			return $state;
		}

		$state['redirect_to'] = $this->redirect_uri;

		return $state;
	}

	/**
	 * Determines whether to process the shortcode.
	 *
	 * @param array $attrs Shortcode attributes.
	 *
	 * @return bool
	 */
	private function should_display( array $attrs ): bool {
		if ( ! is_user_logged_in() || ( ! empty( $attrs['force_display'] ) && 'yes' === (string) $attrs['force_display'] ) ) {
			return true;
		}

		return false;
	}
}