AdminAjaxController.php
3 years ago
MetaController.php
3 years ago
NoticeController.php
3 years ago
PostTypeController.php
3 years ago
SettingsController.php
3 years ago
UpgradeController.php
3 years ago
SettingsController.php
228 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Settings Controller class. |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Controllers\Admin; |
| 9 | |
| 10 | use RT\ThePostGrid\Helpers\Fns; |
| 11 | |
| 12 | // Do not allow directly accessing this file. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit( 'This script cannot be accessed directly.' ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Settings Controller class. |
| 19 | */ |
| 20 | class SettingsController { |
| 21 | /** |
| 22 | * Shortcode tag |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private $sc_tag = 'rt_tpg_scg'; |
| 27 | |
| 28 | /** |
| 29 | * Class init. |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function init() { |
| 34 | add_action( 'admin_menu', [ $this, 'register' ] ); |
| 35 | add_filter( 'plugin_action_links_' . RT_THE_POST_GRID_PLUGIN_ACTIVE_FILE_NAME, [ $this, 'marketing' ] ); |
| 36 | add_action( 'admin_enqueue_scripts', [ $this, 'settings_admin_enqueue_scripts' ] ); |
| 37 | add_action( 'wp_print_styles', [ $this, 'tpg_dequeue_unnecessary_styles' ], 99 ); |
| 38 | add_action( 'admin_footer', [ $this, 'pro_alert_html' ] ); |
| 39 | add_action( 'admin_head', [ $this, 'admin_head' ] ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Calls functions into the correct filters |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function admin_head() { |
| 48 | if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if ( 'true' == get_user_option( 'rich_editing' ) ) { |
| 53 | add_filter( 'mce_external_plugins', [ $this, 'mce_external_plugins' ] ); |
| 54 | add_filter( 'mce_buttons', [ $this, 'mce_buttons' ] ); |
| 55 | echo '<style>'; |
| 56 | echo 'i.mce-i-rt_tpg_scg{'; |
| 57 | echo "background: url('" . esc_url( rtTPG()->get_assets_uri( 'images/icon-20x20.png' ) ) . "');"; |
| 58 | echo '}'; |
| 59 | echo '</style>'; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Adds tinymce plugin |
| 65 | * |
| 66 | * @param array $plugin_array Plugins. |
| 67 | * |
| 68 | * @return array |
| 69 | */ |
| 70 | public function mce_external_plugins( $plugin_array ) { |
| 71 | $plugin_array[ $this->sc_tag ] = rtTPG()->get_assets_uri( 'js/mce-button.js' ); |
| 72 | |
| 73 | return $plugin_array; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Adds tinymce button |
| 78 | * |
| 79 | * @param array $buttons Buttons. |
| 80 | * |
| 81 | * @return array |
| 82 | */ |
| 83 | public function mce_buttons( $buttons ) { |
| 84 | array_push( $buttons, $this->sc_tag ); |
| 85 | |
| 86 | return $buttons; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Pro alert |
| 91 | * |
| 92 | * @return void |
| 93 | */ |
| 94 | public function pro_alert_html() { |
| 95 | global $typenow; |
| 96 | |
| 97 | if ( rtTPG()->hasPro() ) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if ( ( isset( $_GET['page'] ) && 'rttpg_settings' !== $_GET['page'] ) || rtTPG()->post_type !== $typenow ) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | $html = ''; |
| 106 | $html .= '<div class="rt-document-box rt-alert rt-pro-alert"> |
| 107 | <div class="rt-box-icon"><i class="dashicons dashicons-lock"></i></div> |
| 108 | <div class="rt-box-content"> |
| 109 | <h3 class="rt-box-title">' . esc_html__( 'Pro field alert!', 'the-post-grid' ) . '</h3> |
| 110 | <p><span></span>' . esc_html__( 'Sorry! this is a Pro field. To use this field, you need to use Pro plugin.', 'the-post-grid' ) . '</p> |
| 111 | <a href="' . esc_url( rtTpg()->proLink() ) . '" target="_blank" class="rt-admin-btn">' . esc_html__( 'Upgrade to Pro', 'the-post-grid' ) . '</a> |
| 112 | <a href="#" target="_blank" class="rt-alert-close rt-pro-alert-close">x</a> |
| 113 | </div> |
| 114 | </div>'; |
| 115 | |
| 116 | Fns::print_html( $html ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Dequeue styles |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | public function tpg_dequeue_unnecessary_styles() { |
| 125 | $settings = get_option( rtTPG()->options['settings'] ); |
| 126 | |
| 127 | if ( isset( $settings['tpg_skip_fa'] ) ) { |
| 128 | wp_dequeue_style( 'rt-fontawsome' ); |
| 129 | wp_deregister_style( 'rt-fontawsome' ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Admin scripts |
| 135 | * |
| 136 | * @return void |
| 137 | */ |
| 138 | public function settings_admin_enqueue_scripts() { |
| 139 | global $pagenow, $typenow; |
| 140 | |
| 141 | if ( ! in_array( $pagenow, [ 'edit.php' ], true ) ) { |
| 142 | return; |
| 143 | } |
| 144 | if ( rtTPG()->post_type !== $typenow ) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | wp_enqueue_script( 'jquery' ); |
| 149 | wp_enqueue_script( 'rt-tpg-admin' ); |
| 150 | |
| 151 | // styles. |
| 152 | wp_enqueue_style( 'rt-tpg-admin' ); |
| 153 | |
| 154 | $nonce = wp_create_nonce( rtTPG()->nonceText() ); |
| 155 | |
| 156 | wp_localize_script( |
| 157 | 'rt-tpg-admin', |
| 158 | 'rttpg', |
| 159 | [ |
| 160 | 'nonceID' => esc_attr( rtTPG()->nonceId() ), |
| 161 | 'nonce' => esc_attr( $nonce ), |
| 162 | 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ), |
| 163 | ] |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Marketing |
| 169 | * |
| 170 | * @param array $links Links. |
| 171 | * @return array |
| 172 | */ |
| 173 | public function marketing( $links ) { |
| 174 | $links[] = '<a target="_blank" href="' . esc_url( rtTpg()->demoLink() ) . '">Demo</a>'; |
| 175 | $links[] = '<a target="_blank" href="' . esc_url( rtTpg()->docLink() ) . '">Documentation</a>'; |
| 176 | |
| 177 | if ( ! rtTPG()->hasPro() ) { |
| 178 | $links[] = '<a target="_blank" style="color: #39b54a;font-weight: 700;" href="' . esc_url( rtTpg()->proLink() ) . '">Get Pro</a>'; |
| 179 | } |
| 180 | |
| 181 | return $links; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Submenu |
| 186 | * |
| 187 | * @return void |
| 188 | */ |
| 189 | public function register() { |
| 190 | add_submenu_page( |
| 191 | 'edit.php?post_type=' . rtTPG()->post_type, |
| 192 | esc_html__( 'Settings', 'the-post-grid' ), |
| 193 | esc_html__( 'Settings', 'the-post-grid' ), |
| 194 | 'administrator', |
| 195 | 'rttpg_settings', |
| 196 | [ $this, 'settings' ] |
| 197 | ); |
| 198 | |
| 199 | add_submenu_page( |
| 200 | 'edit.php?post_type=' . rtTPG()->post_type, |
| 201 | esc_html__( 'Get Help', 'the-post-grid' ), |
| 202 | esc_html__( 'Get Help', 'the-post-grid' ), |
| 203 | 'administrator', |
| 204 | 'rttpg_get_help', |
| 205 | [ $this, 'get_help' ] |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get help view |
| 211 | * |
| 212 | * @return void |
| 213 | */ |
| 214 | public function get_help() { |
| 215 | Fns::view( 'page.help' ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Settings view |
| 220 | * |
| 221 | * @return void |
| 222 | */ |
| 223 | public function settings() { |
| 224 | Fns::view( 'settings.settings' ); |
| 225 | } |
| 226 | |
| 227 | } |
| 228 |