|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Admin features. |
| 4 | + * |
| 5 | + * @package um_ext\um_polylang\admin |
| 6 | + */ |
| 7 | + |
| 8 | +namespace um_ext\um_polylang\admin; |
| 9 | + |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { |
| 11 | + exit; |
| 12 | +} |
| 13 | + |
| 14 | +if ( ! class_exists( 'um_ext\um_polylang\admin\Admin' ) ) { |
| 15 | + |
| 16 | + |
| 17 | + /** |
| 18 | + * Class Admin. |
| 19 | + * |
| 20 | + * @package um_ext\um_polylang\admin |
| 21 | + */ |
| 22 | + class Admin { |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * Admin constructor. |
| 27 | + */ |
| 28 | + public function __construct() { |
| 29 | + // Notices. |
| 30 | + add_action( 'in_admin_header', array( $this, 'notice_create_forms' ) ); |
| 31 | + add_action( 'in_admin_header', array( $this, 'notice_create_pages' ) ); |
| 32 | + add_filter( 'um_adm_action_custom_notice_update', array( $this, 'notice_update' ), 10, 2 ); |
| 33 | + |
| 34 | + // Create Forms. |
| 35 | + add_action( 'um_admin_do_action__um_pll_create_forms', array( $this, 'action_create_forms' ) ); |
| 36 | + |
| 37 | + // Create Pages. |
| 38 | + add_action( 'um_admin_do_action__um_pll_create_pages', array( $this, 'action_create_pages' ) ); |
| 39 | + |
| 40 | + // Translatable post types. |
| 41 | + add_filter( 'pll_get_post_types', array( $this, 'pll_get_post_types' ), 10, 2 ); |
| 42 | + |
| 43 | + // Settings, Email tab. |
| 44 | + $this->settings_email_tab(); |
| 45 | + |
| 46 | + // Forms table styles. |
| 47 | + add_action( 'admin_footer', array( $this, 'styles' ) ); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * The "Create Forms" button handler. |
| 53 | + */ |
| 54 | + public function action_create_forms() { |
| 55 | + $args = array( |
| 56 | + 'fields' => 'ids', |
| 57 | + 'nopaging' => true, |
| 58 | + 'post_status' => 'publish', |
| 59 | + 'post_type' => 'um_form', |
| 60 | + ); |
| 61 | + $posts = get_posts( $args ); |
| 62 | + |
| 63 | + UM()->Polylang()->setup()->create_posts( $posts, 'um_form' ); |
| 64 | + |
| 65 | + $url = add_query_arg( 'update', 'um_pll_create_forms', admin_url( 'edit.php?post_type=um_form' ) ); |
| 66 | + exit( wp_safe_redirect( $url ) ); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * The "Create Pages" button handler. |
| 72 | + */ |
| 73 | + public function action_create_pages() { |
| 74 | + $posts = UM()->config()->permalinks; |
| 75 | + |
| 76 | + UM()->Polylang()->setup()->create_posts( $posts, 'page' ); |
| 77 | + |
| 78 | + $url = add_query_arg( 'update', 'um_pll_create_pages', admin_url( 'edit.php?post_type=page' ) ); |
| 79 | + exit( wp_safe_redirect( $url ) ); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + /** |
| 84 | + * Display a notice with the "Create Forms" button. |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + public function notice_create_forms() { |
| 89 | + $screen = get_current_screen(); |
| 90 | + if ( ! is_object( $screen ) || 'edit-um_form' !== $screen->id ) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + $languages = pll_languages_list(); |
| 95 | + if ( empty( $languages ) ) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + $args = array( |
| 100 | + 'fields' => 'ids', |
| 101 | + 'nopaging' => true, |
| 102 | + 'post_status' => 'publish', |
| 103 | + 'post_type' => 'um_form', |
| 104 | + ); |
| 105 | + $posts = get_posts( $args ); |
| 106 | + if ( empty( $posts ) ) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + $need_translations = array(); |
| 111 | + foreach ( $posts as $post => $post_id ) { |
| 112 | + $post_translations = pll_get_post_translations( $post_id ); |
| 113 | + if ( array_diff( $languages, array_keys( $post_translations ) ) ) { |
| 114 | + $need_translations[] = $post_id; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if ( $need_translations ) { |
| 120 | + $url_params = array( |
| 121 | + 'um_adm_action' => 'um_pll_create_forms', |
| 122 | + '_wpnonce' => wp_create_nonce( 'um_pll_create_forms' ), |
| 123 | + ); |
| 124 | + |
| 125 | + $url = add_query_arg( $url_params ); |
| 126 | + |
| 127 | + ob_start(); |
| 128 | + ?> |
| 129 | + |
| 130 | + <p> |
| 131 | + <?php |
| 132 | + // translators: %s: Plugin name. |
| 133 | + echo wp_kses( |
| 134 | + sprintf( __( '%s needs to create required forms for every language to function correctly.', 'um-polylang' ), UM_PLUGIN_NAME ), |
| 135 | + UM()->get_allowed_html( 'admin_notice' ) |
| 136 | + ); |
| 137 | + ?> |
| 138 | + </p> |
| 139 | + <p> |
| 140 | + <a href="<?php echo esc_url( $url ); ?>" class="button button-primary"><?php esc_html_e( 'Create Forms', 'um-polylang' ); ?></a> |
| 141 | + <a href="javascript:void(0);" class="button-secondary um_secondary_dismiss"><?php esc_html_e( 'No thanks', 'um-polylang' ); ?></a> |
| 142 | + </p> |
| 143 | + |
| 144 | + <?php |
| 145 | + $message = ob_get_clean(); |
| 146 | + |
| 147 | + $notice_data = array( |
| 148 | + 'class' => 'notice-warning', |
| 149 | + 'message' => $message, |
| 150 | + 'dismissible' => true, |
| 151 | + ); |
| 152 | + |
| 153 | + UM()->admin()->notices()->add_notice( 'um_pll_create_forms', $notice_data, 20 ); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + /** |
| 159 | + * Display a notice with the "Create Pages" button. |
| 160 | + * |
| 161 | + * @return void |
| 162 | + */ |
| 163 | + public function notice_create_pages() { |
| 164 | + $screen = get_current_screen(); |
| 165 | + if ( ! is_object( $screen ) || 'edit-page' !== $screen->id ) { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + $languages = pll_languages_list(); |
| 170 | + if ( empty( $languages ) ) { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + $posts = UM()->config()->permalinks; |
| 175 | + if ( empty( $posts ) ) { |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + $need_translations = array(); |
| 180 | + foreach ( $posts as $post => $post_id ) { |
| 181 | + $post_translations = pll_get_post_translations( $post_id ); |
| 182 | + if ( array_diff( $languages, array_keys( $post_translations ) ) ) { |
| 183 | + $need_translations[] = $post_id; |
| 184 | + break; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if ( $need_translations ) { |
| 189 | + $url_params = array( |
| 190 | + 'um_adm_action' => 'um_pll_create_pages', |
| 191 | + '_wpnonce' => wp_create_nonce( 'um_pll_create_pages' ), |
| 192 | + ); |
| 193 | + |
| 194 | + $url = add_query_arg( $url_params ); |
| 195 | + |
| 196 | + ob_start(); |
| 197 | + ?> |
| 198 | + |
| 199 | + <p> |
| 200 | + <?php |
| 201 | + // translators: %s: Plugin name. |
| 202 | + echo wp_kses( |
| 203 | + sprintf( __( '%s needs to create required pages for every language to function correctly.', 'um-polylang' ), UM_PLUGIN_NAME ), |
| 204 | + UM()->get_allowed_html( 'admin_notice' ) |
| 205 | + ); |
| 206 | + ?> |
| 207 | + </p> |
| 208 | + <p> |
| 209 | + <a href="<?php echo esc_url( $url ); ?>" class="button button-primary"><?php esc_html_e( 'Create Pages', 'um-polylang' ); ?></a> |
| 210 | + <a href="javascript:void(0);" class="button-secondary um_secondary_dismiss"><?php esc_html_e( 'No thanks', 'um-polylang' ); ?></a> |
| 211 | + </p> |
| 212 | + |
| 213 | + <?php |
| 214 | + $message = ob_get_clean(); |
| 215 | + |
| 216 | + $notice_data = array( |
| 217 | + 'class' => 'notice-warning', |
| 218 | + 'message' => $message, |
| 219 | + 'dismissible' => true, |
| 220 | + ); |
| 221 | + |
| 222 | + UM()->admin()->notices()->add_notice( 'um_pll_create_pages', $notice_data, 20 ); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | + /** |
| 228 | + * Display a notice after um_adm_action. |
| 229 | + * |
| 230 | + * @param array $messages Admin notice messages. |
| 231 | + * @param string $update Update action key. |
| 232 | + * |
| 233 | + * @return array |
| 234 | + */ |
| 235 | + public function notice_update( $messages, $update ) { |
| 236 | + |
| 237 | + switch ( $update ) { |
| 238 | + |
| 239 | + case 'um_pll_create_forms': |
| 240 | + $messages[0]['content'] = __( 'Forms have been duplicated successfully.', 'um-polylang' ); |
| 241 | + break; |
| 242 | + |
| 243 | + case 'um_pll_create_pages': |
| 244 | + $messages[0]['content'] = __( 'Pages have been duplicated successfully.', 'um-polylang' ); |
| 245 | + break; |
| 246 | + } |
| 247 | + |
| 248 | + return $messages; |
| 249 | + } |
| 250 | + |
| 251 | + |
| 252 | + /** |
| 253 | + * Filters the list of post types available for translation. |
| 254 | + * |
| 255 | + * @param string[] $post_types List of post type names (as array keys and values). |
| 256 | + * @param bool $is_settings True when displaying the list of custom post types in Polylang settings. |
| 257 | + * |
| 258 | + * @return string[] List of post type names. |
| 259 | + */ |
| 260 | + public function pll_get_post_types( $post_types, $is_settings ) { |
| 261 | + $post_types['um_form'] = 'um_form'; |
| 262 | + return array_unique( $post_types ); |
| 263 | + } |
| 264 | + |
| 265 | + |
| 266 | + /** |
| 267 | + * Extend settings Email tab. |
| 268 | + * |
| 269 | + * @return um_ext\um_polylang\admin\Mail() |
| 270 | + */ |
| 271 | + public function settings_email_tab() { |
| 272 | + if ( empty( UM()->classes['um_polylang_admin_mail'] ) ) { |
| 273 | + require_once 'class-mail.php'; |
| 274 | + UM()->classes['um_polylang_admin_mail'] = new Mail(); |
| 275 | + } |
| 276 | + return UM()->classes['um_polylang_admin_mail']; |
| 277 | + } |
| 278 | + |
| 279 | + |
| 280 | + /** |
| 281 | + * Fix column width in the "Forms" table. |
| 282 | + */ |
| 283 | + public function styles() { |
| 284 | + $screen = get_current_screen(); |
| 285 | + if ( ! is_object( $screen ) || 'edit-um_form' === $screen->id ) { |
| 286 | + ?> |
| 287 | +<style type="text/css"> |
| 288 | + @media screen and (max-width: 1200px) { |
| 289 | + .um-admin.post-type-um_form tr > * { padding-left: 5px; padding-right: 5px; } |
| 290 | + .um-admin.post-type-um_form .manage-column.column-title { width: 180px; } |
| 291 | + .um-admin.post-type-um_form .manage-column.column-id { width: 40px; } |
| 292 | + .um-admin.post-type-um_form .manage-column.column-mode { width: 60px; } |
| 293 | + .um-admin.post-type-um_form .manage-column.column-is_default { width: 50px; } |
| 294 | + .um-admin.post-type-um_form .manage-column.column-shortcode { width: 120px; } |
| 295 | + } |
| 296 | +</style><?php |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | +} |
0 commit comments