| 1 |
<?php |
| 2 |
|
| 3 |
/* Prohibit direct script loading */ |
| 4 |
defined('ABSPATH') || die('No direct script access allowed!'); |
| 5 |
|
| 6 |
/** |
| 7 |
* Class LinguiseMenu |
| 8 |
*/ |
| 9 |
class LinguiseMenu |
| 10 |
{ |
| 11 |
/** |
| 12 |
* LinguiseMenu constructor. |
| 13 |
*/ |
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
add_action('admin_init', array($this, 'adminInit')); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Setups filters and terms |
| 21 |
* adds the language switcher metabox and create new nav menu locations |
| 22 |
* |
| 23 |
* @return void |
| 24 |
* @since 1.1 |
| 25 |
*/ |
| 26 |
public function adminInit() |
| 27 |
{ |
| 28 |
add_action('wp_update_nav_menu_item', array($this, 'wpUpdateNavMenuItem'), 10, 2); |
| 29 |
add_meta_box('linguise_lang_switch_box', __('Linguise switcher', 'linguise'), array($this, 'langSwitch'), 'nav-menus', 'side', 'high'); |
| 30 |
add_filter('get_user_option_metaboxhidden_nav-menus', array($this, 'enableMenuMetaBox'), 10, 3); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Enable menu meta box |
| 35 |
* |
| 36 |
* @param mixed $result Value for the user's option. |
| 37 |
* @param string $option Name of the option being retrieved. |
| 38 |
* @param WP_User $user WP_User object of the user whose option is being retrieved. |
| 39 |
* |
| 40 |
* @return mixed |
| 41 |
*/ |
| 42 |
public function enableMenuMetaBox($result, $option, $user) |
| 43 |
{ |
| 44 |
if (is_array($result) && in_array('linguise_lang_switch_box', $result)) { |
| 45 |
$key = array_search('linguise_lang_switch_box', $result); |
| 46 |
unset($result[$key]); |
| 47 |
} |
| 48 |
return $result; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Language switcher metabox |
| 53 |
* The checkbox and all hidden fields are important |
| 54 |
* Thanks to John Morris for his very interesting post http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/ |
| 55 |
* |
| 56 |
* @return void |
| 57 |
* @since 1.1 |
| 58 |
*/ |
| 59 |
public function langSwitch() |
| 60 |
{ |
| 61 |
global $_nav_menu_placeholder, $nav_menu_selected_id; |
| 62 |
$_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Force it |
| 63 |
?> |
| 64 |
<div id="posttype-lang-switch" class="posttypediv"> |
| 65 |
<div id="tabs-panel-lang-switch" class="tabs-panel tabs-panel-active"> |
| 66 |
<ul id="lang-switch-checklist" class="categorychecklist form-no-clear"> |
| 67 |
<li> |
| 68 |
<label class="menu-item-title"> |
| 69 |
<input type="checkbox" class="menu-item-checkbox" |
| 70 |
name="menu-item[<?php echo (int)$_nav_menu_placeholder; ?>][menu-item-object-id]" |
| 71 |
value="-1"> <?php esc_html_e('Linguise Languages', 'linguise'); ?> |
| 72 |
</label> |
| 73 |
<input type="hidden" class="menu-item-type" |
| 74 |
name="menu-item[<?php echo (int)$_nav_menu_placeholder; ?>][menu-item-type]" |
| 75 |
value="custom"> |
| 76 |
<input type="hidden" class="menu-item-title" |
| 77 |
name="menu-item[<?php echo (int)$_nav_menu_placeholder; ?>][menu-item-title]" |
| 78 |
value="<?php esc_html_e('Linguise Languages', 'linguise'); ?>"> |
| 79 |
<input type="hidden" class="menu-item-url" |
| 80 |
name="menu-item[<?php echo (int)$_nav_menu_placeholder; ?>][menu-item-url]" |
| 81 |
value="#linguise_switcher"> |
| 82 |
</li> |
| 83 |
</ul> |
| 84 |
</div> |
| 85 |
<p class="button-controls"> |
| 86 |
<span class="add-to-menu"> |
| 87 |
<input type="submit" <?php disabled($nav_menu_selected_id, 0); ?> class="button-secondary submit-add-to-menu right" |
| 88 |
value="<?php esc_attr_e('Add to Menu', 'linguise'); ?>" name="add-post-type-menu-item" |
| 89 |
id="submit-posttype-lang-switch"> |
| 90 |
<span class="spinner"></span> |
| 91 |
</span> |
| 92 |
</p> |
| 93 |
</div> |
| 94 |
<?php |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Save our menu item options |
| 99 |
* |
| 100 |
* @param integer $menu_id Not used |
| 101 |
* @param integer $menu_item_db_id Menu item db Id |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function wpUpdateNavMenuItem($menu_id = 0, $menu_item_db_id = 0) |
| 106 |
{ |
| 107 |
if (empty($_POST['menu-item-url'][$menu_item_db_id]) || '#linguise_switcher' !== $_POST['menu-item-url'][$menu_item_db_id]) { // phpcs:ignore WordPress.Security.NonceVerification |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
// Security check as 'wpUpdateNavMenuItem' can be called from outside WP admin |
| 112 |
if (current_user_can('edit_theme_options')) { |
| 113 |
check_admin_referer('update-nav_menu', 'update-nav-menu-nonce'); |
| 114 |
|
| 115 |
$options = linguiseGetOptions(); |
| 116 |
// Our jQuery form has not been displayed |
| 117 |
if (empty($_POST['menu-item-linguise-detect'][$menu_item_db_id])) { |
| 118 |
if (!get_post_meta($menu_item_db_id, '_linguise_menu_item', true)) { // Our options were never saved |
| 119 |
update_post_meta($menu_item_db_id, '_linguise_menu_item', $options); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
new LinguiseMenu; |
| 127 |
|