'', 'label' => '', 'enabled' => 1 ) ); } function hadeel_valid_key( $key ) { return (bool) preg_match( '/^wk_[A-Za-z0-9_-]{6,64}$/', (string) $key ); } /* ---------- front end: the launcher script on every page ---------- */ add_action( 'wp_enqueue_scripts', 'hadeel_enqueue' ); function hadeel_enqueue() { $o = hadeel_get_options(); if ( empty( $o['enabled'] ) || ! hadeel_valid_key( $o['key'] ) ) { return; } $args = version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ? array( 'in_footer' => true, 'strategy' => 'async' ) : true; wp_enqueue_script( 'hadeel-launcher', HADEEL_SERVICE . '/launcher.js', array(), null, $args ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- the service versions its own file } add_filter( 'script_loader_tag', 'hadeel_script_tag', 10, 2 ); function hadeel_script_tag( $tag, $handle ) { if ( 'hadeel-launcher' !== $handle ) { return $tag; } $o = hadeel_get_options(); $attrs = ' data-key="' . esc_attr( $o['key'] ) . '"'; if ( '' !== trim( (string) $o['label'] ) ) { $attrs .= ' data-label="' . esc_attr( $o['label'] ) . '"'; } if ( false === strpos( $tag, ' async' ) ) { $attrs .= ' async'; } return str_replace( ' src=', $attrs . ' src=', $tag ); } /* ---------- admin: settings page under Settings → Hadeel ---------- */ add_action( 'admin_menu', 'hadeel_menu' ); function hadeel_menu() { add_options_page( hadeel_t( 'هديل', 'Hadeel' ), hadeel_t( 'هديل', 'Hadeel' ), 'manage_options', 'hadeel', 'hadeel_settings_page' ); } add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'hadeel_action_links' ); function hadeel_action_links( $links ) { array_unshift( $links, '' . esc_html( hadeel_t( 'الإعدادات', 'Settings' ) ) . '' ); return $links; } add_action( 'admin_init', 'hadeel_admin_init' ); function hadeel_admin_init() { register_setting( 'hadeel', 'hadeel_options', array( 'sanitize_callback' => 'hadeel_sanitize' ) ); // Coming back from hadeel.app/app/#/wp-connect: the key arrives with our own nonce, on an admin's request. if ( isset( $_GET['page'], $_GET['hadeel_key'], $_GET['hadeel_state'] ) && 'hadeel' === sanitize_key( wp_unslash( $_GET['page'] ) ) && current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- the nonce is hadeel_state, verified below before anything is saved $state = sanitize_text_field( wp_unslash( $_GET['hadeel_state'] ) ); $key = sanitize_text_field( wp_unslash( $_GET['hadeel_key'] ) ); if ( wp_verify_nonce( $state, 'hadeel_connect' ) && hadeel_valid_key( $key ) ) { $o = hadeel_get_options(); $o['key'] = $key; $o['enabled'] = 1; update_option( 'hadeel_options', $o ); wp_safe_redirect( admin_url( 'options-general.php?page=hadeel&hadeel_connected=1' ) ); exit; } wp_safe_redirect( admin_url( 'options-general.php?page=hadeel&hadeel_connected=0' ) ); exit; } } function hadeel_sanitize( $in ) { $in = is_array( $in ) ? $in : array(); $key = isset( $in['key'] ) ? trim( sanitize_text_field( $in['key'] ) ) : ''; if ( '' !== $key && ! hadeel_valid_key( $key ) ) { add_settings_error( 'hadeel_options', 'hadeel_bad_key', hadeel_t( 'مفتاح الويدجت غير صحيح. يبدأ بـ wk_ وتلقاه في هديل ← القنوات ← هديل على موقعك.', 'The widget key is not valid. It starts with wk_ — find it in Hadeel → Channels → Hadeel on your website.' ) ); $key = hadeel_get_options()['key']; } return array( 'key' => $key, 'label' => isset( $in['label'] ) ? mb_substr( sanitize_text_field( $in['label'] ), 0, 40 ) : '', 'enabled' => empty( $in['enabled'] ) ? 0 : 1, ); } function hadeel_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $o = hadeel_get_options(); $return = admin_url( 'options-general.php?page=hadeel' ); $connect = HADEEL_SERVICE . '/app/#/wp-connect?' . http_build_query( array( 'return' => $return, 'state' => wp_create_nonce( 'hadeel_connect' ) ) ); $is_rtl = is_rtl(); ?>