astra.php
90 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Astra Compatibility. |
| 4 | * |
| 5 | * @package sureforms |
| 6 | * @since 1.6.1 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc\Compatibility\Themes; |
| 10 | |
| 11 | use SRFM\Inc\Helper; |
| 12 | use SRFM\Inc\Traits\Get_Instance; |
| 13 | |
| 14 | // Exit if accessed directly. |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Sureforms Astra Compatibility. |
| 19 | * |
| 20 | * @since 1.6.1 |
| 21 | */ |
| 22 | class Astra { |
| 23 | use Get_Instance; |
| 24 | |
| 25 | /** |
| 26 | * Constructor |
| 27 | * |
| 28 | * @since 1.6.1 |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | // Update Astra's menu priority to show after Dashboard menu. |
| 32 | add_filter( 'astra_menu_priority', [ $this, 'update_admin_menu_position' ], 99999, 1 ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Update Astra's menu priority to show after Dashboard menu. |
| 37 | * Checks for existing menu items and assigns a new priority to Astra's menu item. |
| 38 | * |
| 39 | * @param int $astra_priority Astra menu priority. |
| 40 | * |
| 41 | * @return float |
| 42 | * @since 1.6.1 |
| 43 | */ |
| 44 | public function update_admin_menu_position( $astra_priority ) { |
| 45 | global $menu; |
| 46 | |
| 47 | $dashboard_priority = null; |
| 48 | |
| 49 | // Loop through the menu items to find the dashboard priority. |
| 50 | foreach ( $menu as $position => $menu_item ) { |
| 51 | // We are checking at index 5 because it is the position of the menu-dashboard slug and it is not affected by translation. |
| 52 | if ( isset( $menu_item[5] ) && 'menu-dashboard' === $menu_item[5] ) { |
| 53 | $dashboard_priority = (float) $position; |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // If we couldn't find the dashboard priority, set it to 2.0. |
| 59 | if ( null === $dashboard_priority ) { |
| 60 | $dashboard_priority = 2.0; |
| 61 | } |
| 62 | |
| 63 | // Check if the Astra priority is numeric. |
| 64 | // If not, set it to 2.0. |
| 65 | if ( ! is_numeric( $astra_priority ) ) { |
| 66 | $astra_priority = 2.0; |
| 67 | } else { |
| 68 | $astra_priority = (float) $astra_priority; |
| 69 | } |
| 70 | |
| 71 | // If the Astra priority is already less than the dashboard priority, return it. |
| 72 | if ( $astra_priority < $dashboard_priority ) { |
| 73 | return $astra_priority; |
| 74 | } |
| 75 | |
| 76 | // Increase the Astra priority by 0.1 so that it is placed right after the Dashboard. |
| 77 | $astra_priority = $dashboard_priority + 0.1; |
| 78 | |
| 79 | $existing_priorities = array_keys( $menu ); |
| 80 | |
| 81 | // If the computed astra_priority already exists, just default to 2.0. |
| 82 | // We are converting the existing priorities to string to make sure the type is always same. |
| 83 | if ( in_array( Helper::get_string_value( $astra_priority ), array_map( 'strval', $existing_priorities ), true ) ) { |
| 84 | $astra_priority = 2.0; |
| 85 | } |
| 86 | |
| 87 | return $astra_priority; |
| 88 | } |
| 89 | } |
| 90 |