Code_Manager.php
4 years ago
Code_Manager_Dashboard.php
4 years ago
Code_Manager_Export.php
4 years ago
Code_Manager_Form.php
4 years ago
Code_Manager_Import.php
4 years ago
Code_Manager_Import_File.php
4 years ago
Code_Manager_List.php
4 years ago
Code_Manager_List_View.php
4 years ago
Code_Manager_Model.php
4 years ago
Code_Manager_Preview.php
4 years ago
Code_Manager_Settings.php
4 years ago
Code_Manager_Tabs.php
4 years ago
Message_Box.php
4 years ago
WP_List_Table.php
4 years ago
Code_Manager_Preview.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Code_Manager { |
| 4 | |
| 5 | class Code_Manager_Preview { |
| 6 | |
| 7 | /** |
| 8 | * Holds all current user/code_id combinations that are preview enabled |
| 9 | * |
| 10 | * @var array |
| 11 | */ |
| 12 | private static $preview = []; |
| 13 | private static $preview_loaded = false; |
| 14 | |
| 15 | /** |
| 16 | * Load preview code ids for current user |
| 17 | */ |
| 18 | private static function load_user_preview_codes() { |
| 19 | if ( ! self::$preview_loaded ) { |
| 20 | $code_manager_preview_code_ids = get_user_meta( |
| 21 | get_current_user_id(), |
| 22 | 'code_manager_preview_code_ids', |
| 23 | true |
| 24 | ); |
| 25 | |
| 26 | if ( false !== $code_manager_preview_code_ids && is_array( $code_manager_preview_code_ids ) ) { |
| 27 | self::$preview = $code_manager_preview_code_ids; |
| 28 | } |
| 29 | |
| 30 | self::$preview_loaded = true; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Return preview code ids for current user |
| 36 | * |
| 37 | * @return array |
| 38 | */ |
| 39 | public static function get_user_preview_codes() { |
| 40 | self::load_user_preview_codes(); |
| 41 | |
| 42 | return self::$preview; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Check if preview for given code id is enabled for current user |
| 47 | * |
| 48 | * @param $code_id |
| 49 | */ |
| 50 | public static function is_code_id_preview_enabled( $code_id ) { |
| 51 | self::load_user_preview_codes(); |
| 52 | |
| 53 | return is_array( self::$preview ) && in_array( $code_id, self::$preview ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Enable preview mode for given code id for current user |
| 58 | * |
| 59 | * @param $code_id |
| 60 | */ |
| 61 | public static function add_user_preview_code_id( $code_id ) { |
| 62 | if ( is_admin() ) { |
| 63 | self::load_user_preview_codes(); |
| 64 | |
| 65 | if ( is_array( self::$preview ) && ! in_array( $code_id, self::$preview ) ) { |
| 66 | array_push( self::$preview, $code_id ); |
| 67 | |
| 68 | update_user_meta( get_current_user_id(), 'code_manager_preview_code_ids', self::$preview ); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Disable preview mode for given code id for current user |
| 75 | * |
| 76 | * @param $code_id |
| 77 | */ |
| 78 | public static function remove_user_preview_code_id( $code_id ) { |
| 79 | if ( is_admin() ) { |
| 80 | self::load_user_preview_codes(); |
| 81 | |
| 82 | if ( is_array( self::$preview ) ) { |
| 83 | if ( ( $key = array_search( $code_id, self::$preview ) ) !== false ) { |
| 84 | unset( self::$preview[ $key ] ); |
| 85 | } |
| 86 | |
| 87 | update_user_meta( get_current_user_id(), 'code_manager_preview_code_ids', self::$preview ); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | } |