| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Beyondwords\Wordpress\Core; |
| 6 |
|
| 7 |
/** |
| 8 |
* BeyondWords Core Utilities. |
| 9 |
* |
| 10 |
* @package Beyondwords |
| 11 |
* @subpackage Beyondwords/includes |
| 12 |
* @author Stuart McAlpine <stu@beyondwords.io> |
| 13 |
* @since 3.5.0 |
| 14 |
*/ |
| 15 |
class CoreUtils |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Check to see if the Gutenberg Editor is being used. |
| 19 |
* |
| 20 |
* @link https://wordpress.stackexchange.com/a/324866 |
| 21 |
* |
| 22 |
* @since 3.0.0 |
| 23 |
* @since 3.5.0 Moved from Core\Utils to Core\CoreUtils |
| 24 |
*/ |
| 25 |
public static function isGutenbergPage() |
| 26 |
{ |
| 27 |
if (function_exists('is_gutenberg_page') && is_gutenberg_page()) { |
| 28 |
// The Gutenberg plugin is on. |
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
$currentScreen = null; |
| 33 |
|
| 34 |
if (function_exists('get_current_screen')) { |
| 35 |
$currentScreen = get_current_screen(); |
| 36 |
} |
| 37 |
|
| 38 |
if ($currentScreen === null) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
if (method_exists($currentScreen, 'is_block_editor') && $currentScreen->is_block_editor()) { |
| 43 |
// Gutenberg page on 5+. |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Check to see if current screen is an edit screen |
| 52 |
* (this includes the screen that lists the posts). |
| 53 |
* |
| 54 |
* @since 4.0.0 |
| 55 |
* @since 4.0.5 Ensure is_admin() and $screen |
| 56 |
*/ |
| 57 |
public static function isEditScreen() |
| 58 |
{ |
| 59 |
if (! is_admin()) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
if (! function_exists('get_current_screen')) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
$screen = get_current_screen(); |
| 68 |
|
| 69 |
if (! $screen || ! ($screen instanceof \WP_Screen)) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
// TODO improve this check? |
| 74 |
if ($screen->parent_base === 'edit' || $screen->base === 'post') { |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the BeyondWords post meta keys. |
| 83 |
* |
| 84 |
* @since 4.1.0 |
| 85 |
* |
| 86 |
* @param string $type Type (current|deprecated|all). |
| 87 |
* |
| 88 |
* @throws Exception |
| 89 |
* |
| 90 |
* @return string[] Post meta keys. |
| 91 |
**/ |
| 92 |
public static function getPostMetaKeys($type = 'current') |
| 93 |
{ |
| 94 |
$current = [ |
| 95 |
'beyondwords_generate_audio', |
| 96 |
'beyondwords_project_id', |
| 97 |
'beyondwords_content_id', |
| 98 |
'beyondwords_player_style', |
| 99 |
'beyondwords_language_id', |
| 100 |
'beyondwords_title_voice_id', |
| 101 |
'beyondwords_body_voice_id', |
| 102 |
'beyondwords_summary_voice_id', |
| 103 |
'beyondwords_error_message', |
| 104 |
'beyondwords_disabled', |
| 105 |
]; |
| 106 |
|
| 107 |
$deprecated = [ |
| 108 |
'beyondwords_podcast_id', |
| 109 |
'beyondwords_hash', |
| 110 |
'publish_post_to_speechkit', |
| 111 |
'speechkit_hash', |
| 112 |
'speechkit_generate_audio', |
| 113 |
'speechkit_project_id', |
| 114 |
'speechkit_podcast_id', |
| 115 |
'speechkit_error_message', |
| 116 |
'speechkit_disabled', |
| 117 |
'speechkit_access_key', |
| 118 |
'speechkit_error', |
| 119 |
'speechkit_info', |
| 120 |
'speechkit_response', |
| 121 |
'speechkit_retries', |
| 122 |
'speechkit_updated_at', |
| 123 |
'_speechkit_link', |
| 124 |
'_speechkit_text', |
| 125 |
]; |
| 126 |
|
| 127 |
$keys = []; |
| 128 |
|
| 129 |
switch ($type) { |
| 130 |
case 'current': |
| 131 |
$keys = $current; |
| 132 |
break; |
| 133 |
case 'deprecated': |
| 134 |
$keys = $deprecated; |
| 135 |
break; |
| 136 |
case 'all': |
| 137 |
$keys = array_merge($current, $deprecated); |
| 138 |
break; |
| 139 |
default: |
| 140 |
throw \Exception('Unexpected $type param for CoreUtils::getPostMetaKeys()'); |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
return $keys; |
| 145 |
} |
| 146 |
} |
| 147 |
|