<?php
namespace NeurogenesisTheme\Traits;

trait HasAction {    
    /**
     * Check action nonce
     *
     * @return boolean
     */
    public function checkNonce($action, $get = false)
    {
        if($get) {
            return isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], $action);
        } else {
            return isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], $action);            
        }
    }
    
    /**
     * Nonce invalid standard message
     *
     * @param  string $message
     * @param  string $status
     * @return void
     */
    public function nonceInvalidMessage(string $message = null, $status = 'error')
    {
        if( $message != null ) {
            neurogenesis_theme_flash_message_add($message, $status);
        } else {
            neurogenesis_theme_flash_message_add(__('There was a problem with your request. Nonce seems invalid.', 'thinko'), 'error');
        }
    }
    
    /**
     * Redirect to an admin page
     *
     * @param  string $url
     * @return void
     */
    public function redirectToSettingsPage(string $url = null, $args = [])
    {        
        if( $url != null ) {            
            wp_safe_redirect(neurogenesis_theme_generate_page_url($url, $args));
        } else {
            wp_safe_redirect($_POST['_wp_http_referer']);
        }
        exit;
    }
}