| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.22 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Shortcodes; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Shortcodes |
| 20 |
{ |
| 21 |
/** |
| 22 |
* A list of shortcodes. |
| 23 |
* |
| 24 |
* To run shortcode: [loginForm]. i.e. [fboxLoginForm] |
| 25 |
* |
| 26 |
* @var array. |
| 27 |
*/ |
| 28 |
private $shortcodes = [ |
| 29 |
'LoginForm', |
| 30 |
'NavigationMenu' |
| 31 |
]; |
| 32 |
|
| 33 |
/** |
| 34 |
* Shortcodes prefix |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private $prefix = 'fbox'; |
| 39 |
|
| 40 |
public function __construct() |
| 41 |
{ |
| 42 |
$this->addShortcodes(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Adds all shortcodes |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
private function addShortcodes() |
| 51 |
{ |
| 52 |
// redirect shortcodes |
| 53 |
foreach ($this->shortcodes as $shortcode) |
| 54 |
{ |
| 55 |
$shortcode = $this->prefix . $shortcode; |
| 56 |
|
| 57 |
$method = $shortcode . 'ShortcodeHandler'; |
| 58 |
|
| 59 |
// make sure callback exists |
| 60 |
if (!method_exists($this, $method)) |
| 61 |
{ |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
// add shortcode |
| 66 |
add_shortcode($shortcode, [$this, $method]); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Create a shortcode [fboxLoginForm] to print the WP Login Form |
| 72 |
* |
| 73 |
* @param array $attributes |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function fboxLoginFormShortcodeHandler($attributes) |
| 78 |
{ |
| 79 |
if (is_user_logged_in()) |
| 80 |
{ |
| 81 |
$factory = new \FPFramework\Base\Factory(); |
| 82 |
$user = $factory->getUser(); |
| 83 |
|
| 84 |
$firstname = $user->user_firstname; |
| 85 |
$lastname = $user->user_lastname; |
| 86 |
|
| 87 |
$sep = empty($lastname) ? '' : ' '; |
| 88 |
|
| 89 |
$name = $firstname . $sep . $lastname; |
| 90 |
$name = empty($name) ? $user->user_login : $name; |
| 91 |
|
| 92 |
$str = '<p>' . fpframework()->_('FPF_HI') . ' ' . esc_attr($name) . ',</p><p><a href="' . wp_logout_url() . '" class="fb-btn fb-btn-primary fb-fullwidth">' . fpframework()->_('FPF_LOG_OUT') . '</a></p>'; |
| 93 |
return $str; |
| 94 |
} |
| 95 |
|
| 96 |
$show_forgot_password_link = isset($attributes['show_forgot_link']) && $attributes['show_forgot_link']; |
| 97 |
$redirect = isset($attributes['redirect']) ? $attributes['redirect'] : (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 98 |
|
| 99 |
$args = [ |
| 100 |
'echo' => false, |
| 101 |
'remember' => true, |
| 102 |
/** |
| 103 |
* We can only redirect to local URLs due to WP using wp_safe_redirect. |
| 104 |
* To redirect to external URLs, users need to hook on allowed_redirect_hosts and add their external URL hosts. |
| 105 |
* Hook doc: https://developer.wordpress.org/reference/hooks/allowed_redirect_hosts/ |
| 106 |
*/ |
| 107 |
'redirect' => $redirect, |
| 108 |
'form_id' => 'fboxLogin', |
| 109 |
'id_username' => 'user_login', |
| 110 |
'id_password' => 'user_pass', |
| 111 |
'id_remember' => 'rememberme', |
| 112 |
'id_submit' => 'wp-submit', |
| 113 |
'label_username' => firebox()->_('FB_USERNAME_OR_EMAIL_ADDRESS'), |
| 114 |
'label_password' => fpframework()->_('FPF_PASSWORD'), |
| 115 |
'label_remember' => fpframework()->_('FPF_REMEMBER_ME'), |
| 116 |
'label_log_in' => fpframework()->_('FPF_LOG_IN'), |
| 117 |
'value_username' => '', |
| 118 |
'value_remember' => false |
| 119 |
]; |
| 120 |
|
| 121 |
$forgot_password_link = $show_forgot_password_link ? '<div class="form-actions"><a href="' . wp_lostpassword_url() . '">' . fpframework()->_('FPF_FORGOT_YOUR_PASSWORD') . '</a></div>' : ''; |
| 122 |
|
| 123 |
// load CSS |
| 124 |
wp_enqueue_style( |
| 125 |
'firebox-login-shortcode', |
| 126 |
FBOX_MEDIA_PUBLIC_URL . 'css/shortcodes/login.css', |
| 127 |
[], |
| 128 |
FBOX_VERSION, |
| 129 |
false |
| 130 |
); |
| 131 |
|
| 132 |
return wp_login_form($args) . $forgot_password_link; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Create a shortcode [fboxNavigationMenu] to print the navigation menu |
| 137 |
* |
| 138 |
* @param array $attributes |
| 139 |
* @param string $content |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
public function fboxNavigationMenuShortcodeHandler($attributes, $content = null) |
| 144 |
{ |
| 145 |
$atts = shortcode_atts( |
| 146 |
[ |
| 147 |
'menu' => '', |
| 148 |
'container' => 'div', |
| 149 |
'container_class' => '', |
| 150 |
'container_id' => '', |
| 151 |
'menu_class' => 'menu', |
| 152 |
'menu_id' => '', |
| 153 |
'echo' => true, |
| 154 |
'fallback_cb' => 'wp_page_menu', |
| 155 |
'before' => '', |
| 156 |
'after' => '', |
| 157 |
'link_before' => '', |
| 158 |
'link_after' => '', |
| 159 |
'depth' => 0, |
| 160 |
'walker' => '', |
| 161 |
'theme_location' => '' |
| 162 |
], |
| 163 |
$attributes, |
| 164 |
'fboxNavigationMenu' |
| 165 |
); |
| 166 |
|
| 167 |
return wp_nav_menu( |
| 168 |
[ |
| 169 |
'menu' => $atts['menu'], |
| 170 |
'container' => $atts['container'], |
| 171 |
'container_class' => $atts['container_class'], |
| 172 |
'container_id' => $atts['container_id'], |
| 173 |
'menu_class' => $atts['menu_class'], |
| 174 |
'menu_id' => $atts['menu_id'], |
| 175 |
'echo' => false, |
| 176 |
'fallback_cb' => $atts['fallback_cb'], |
| 177 |
'before' => $atts['before'], |
| 178 |
'after' => $atts['after'], |
| 179 |
'link_before' => $atts['link_before'], |
| 180 |
'link_after' => $atts['link_after'], |
| 181 |
'depth' => $atts['depth'], |
| 182 |
'walker' => $atts['walker'], |
| 183 |
'theme_location' => $atts['theme_location'] |
| 184 |
] |
| 185 |
); |
| 186 |
} |
| 187 |
} |