elementor
1 year ago
class-simple-author-box-admin-page.php
1 month ago
class-simple-author-box-block.php
1 year ago
class-simple-author-box-helper.php
1 year ago
class-simple-author-box-previewer.php
1 year ago
class-simple-author-box-social.php
1 year ago
class-simple-author-box-user-profile.php
1 year ago
class-simple-author-box-widget.php
1 year ago
class-simple-author-box.php
3 months ago
functions.php
3 years ago
class-simple-author-box-block.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | class Simple_Author_Box_Block { |
| 4 | |
| 5 | /** |
| 6 | * Function constructor |
| 7 | */ |
| 8 | function __construct() { |
| 9 | |
| 10 | // Return early if this function does not exist. |
| 11 | if ( ! function_exists( 'register_block_type' ) ) { |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | add_action( 'init', array( $this, 'register_block_type' ) ); |
| 16 | add_action( 'init', array( $this, 'generate_js_vars' ) ); |
| 17 | add_action( 'wp_ajax_sab_get_author', array( $this, 'get_author' ) ); |
| 18 | } |
| 19 | |
| 20 | public function register_block_type() { |
| 21 | |
| 22 | wp_register_script( 'sab_gutenberg_editor_script', SIMPLE_AUTHOR_BOX_ASSETS . 'js/sab_gutenberg_editor_script.js', array( 'wp-blocks', 'wp-element', 'wp-editor' ) ); |
| 23 | wp_register_style( 'sab_gutenberg_editor_style', SIMPLE_AUTHOR_BOX_ASSETS . 'css/sab_gutenberg_editor_style.css', array() ); |
| 24 | |
| 25 | register_block_type( |
| 26 | 'simple-author-box/sab', |
| 27 | array( |
| 28 | 'render_callback' => array( $this, 'render_sab_block' ), |
| 29 | 'editor_script' => 'sab_gutenberg_editor_script', |
| 30 | 'editor_style' => 'sab_gutenberg_editor_style', |
| 31 | ) |
| 32 | ); |
| 33 | |
| 34 | } |
| 35 | |
| 36 | public function generate_js_vars() { |
| 37 | |
| 38 | $user = wp_get_current_user(); |
| 39 | $padding_top_bottom = Simple_Author_Box_Helper::get_option( 'sab_box_padding_top_bottom' ); |
| 40 | $padding_left_right = Simple_Author_Box_Helper::get_option( 'sab_box_padding_left_right' ); |
| 41 | $top_margin = Simple_Author_Box_Helper::get_option( 'sab_box_margin_top' ); |
| 42 | $sabox_name_size = Simple_Author_Box_Helper::get_option( 'sab_box_name_size' ); |
| 43 | $sabox_desc_size = Simple_Author_Box_Helper::get_option( 'sab_box_desc_size' ); |
| 44 | $sabox_web_size = Simple_Author_Box_Helper::get_option( 'sab_box_web_size' ); |
| 45 | $sabox_icon_size = Simple_Author_Box_Helper::get_option( 'sab_box_icon_size' ); |
| 46 | $sab_box_name_font = Simple_Author_Box_Helper::get_option( 'sab_box_name_font' ); |
| 47 | $sab_box_desc_font = Simple_Author_Box_Helper::get_option( 'sab_box_desc_font' ); |
| 48 | $sab_box_web_font = Simple_Author_Box_Helper::get_option( 'sab_box_web_font' ); |
| 49 | $sab_box_subset = Simple_Author_Box_Helper::get_option( 'sab_box_subset' ); |
| 50 | $bottom_margin = Simple_Author_Box_Helper::get_option( 'sab_box_margin_bottom' ); |
| 51 | $sabox_options = Simple_Author_Box_Helper::get_option( 'saboxplugin_options' ); |
| 52 | $authors = get_users( |
| 53 | array( |
| 54 | 'role__in' => array( 'administrator', 'editor', 'author', 'contributor' ), |
| 55 | 'fields' => array( 'ID', 'display_name' ), |
| 56 | ) |
| 57 | ); |
| 58 | |
| 59 | wp_localize_script( |
| 60 | 'sab_gutenberg_editor_script', |
| 61 | 'sabVars', |
| 62 | array( |
| 63 | 'ajaxURL' => admin_url( 'admin-ajax.php' ), |
| 64 | 'currentUserID' => get_current_user_id(), |
| 65 | 'currentUserRoles' => $user->roles, |
| 66 | 'adminURL' => admin_url(), |
| 67 | 'sab_avatar_style' => isset( $sabox_options['sab_avatar_style'] ) ? $sabox_options['sab_avatar_style'] : '0', |
| 68 | 'sab_avatar_size' => isset( $sabox_options['sab_avatar_size'] ) ? $sabox_options['sab_avatar_size'] : '100', |
| 69 | 'sab_box_long_shadow' => isset( $sabox_options['sab_box_long_shadow'] ) ? $sabox_options['sab_box_long_shadow'] : '0', |
| 70 | 'sab_box_thin_border' => isset( $sabox_options['sab_box_thin_border'] ) ? $sabox_options['sab_box_thin_border'] : '0', |
| 71 | 'sab_box_border' => isset( $sabox_options['sab_box_border'] ) ? $sabox_options['sab_box_border'] : '', |
| 72 | 'sab_box_border_width' => isset( $sabox_options['sab_box_border_width'] ) ? $sabox_options['sab_box_border_width'] : '1', |
| 73 | 'sab_box_icons_back' => isset( $sabox_options['sab_box_icons_back'] ) ? $sabox_options['sab_box_icons_back'] : '', |
| 74 | 'sab_box_author_back' => isset( $sabox_options['sab_box_author_back'] ) ? $sabox_options['sab_box_author_back'] : '', |
| 75 | 'sab_box_author_p_color' => isset( $sabox_options['sab_box_author_p_color'] ) ? $sabox_options['sab_box_author_p_color'] : '', |
| 76 | 'sab_box_author_a_color' => isset( $sabox_options['sab_box_author_a_color'] ) ? $sabox_options['sab_box_author_a_color'] : '', |
| 77 | 'sab_box_author_color' => isset( $sabox_options['sab_box_author_color'] ) ? $sabox_options['sab_box_author_color'] : '', |
| 78 | 'sab_box_web_color' => isset( $sabox_options['sab_box_web_color'] ) ? $sabox_options['sab_box_web_color'] : '', |
| 79 | 'sab_box_name_font' => isset( $sab_box_name_font ) ? $sab_box_name_font : 'None', |
| 80 | 'sab_box_desc_font' => isset( $sab_box_desc_font ) ? $sab_box_desc_font : 'None', |
| 81 | 'sab_box_web_font' => isset( $sab_box_web_font ) ? $sab_box_web_font : 'None', |
| 82 | 'sab_box_subset' => isset( $sab_box_subset ) ? $sab_box_subset : 'none', |
| 83 | 'sab_desc_style' => isset( $sabox_options['sab_desc_style'] ) ? $sabox_options['sab_desc_style'] : '0', |
| 84 | 'padding_top_bottom' => isset( $padding_top_bottom ) ? $padding_top_bottom : '', |
| 85 | 'padding_left_right' => isset( $padding_left_right ) ? $padding_left_right : '', |
| 86 | 'top_margin' => isset( $top_margin ) ? $top_margin : '', |
| 87 | 'bottom_margin' => isset( $bottom_margin ) ? $bottom_margin : '', |
| 88 | 'sabox_name_size' => isset( $sabox_name_size ) ? $sabox_name_size : '18', |
| 89 | 'sabox_desc_size' => isset( $sabox_desc_size ) ? $sabox_desc_size : '14', |
| 90 | 'sabox_web_size' => isset( $sabox_web_size ) ? $sabox_web_size : '14', |
| 91 | 'sabox_icon_size' => isset( $sabox_icon_size ) ? $sabox_icon_size : '18', |
| 92 | 'sab_web' => isset( $sabox_options['sab_web'] ) ? $sabox_options['sab_web'] : 0, |
| 93 | 'sab_hide_socials' => isset( $sabox_options['sab_hide_socials'] ) ? $sabox_options['sab_hide_socials'] : 0, |
| 94 | 'sab_colored' => isset( $sabox_options['sab_colored'] ) ? $sabox_options['sab_colored'] : 0, |
| 95 | 'sab_icons_style' => isset( $sabox_options['sab_icons_style'] ) ? $sabox_options['sab_icons_style'] : 0, |
| 96 | 'authors' => $authors, |
| 97 | 'nonce' => wp_create_nonce( 'sab_nonce' ), |
| 98 | ) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | public function get_author() { |
| 103 | if ( !isset($_POST['nonce']) || !wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['nonce'])), 'sab_nonce' ) ) { |
| 104 | wp_send_json_error(); |
| 105 | die(); |
| 106 | } |
| 107 | |
| 108 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 109 | wp_send_json_error(); |
| 110 | die(); |
| 111 | } |
| 112 | |
| 113 | if(!isset($_POST['author_ID'])){ |
| 114 | wp_send_json_error(); |
| 115 | die(); |
| 116 | } |
| 117 | |
| 118 | $author_id = absint($_POST['author_ID']); |
| 119 | |
| 120 | $author = get_user_by( 'ID', $author_id ); |
| 121 | $author_meta = get_user_meta( $author_id ); |
| 122 | |
| 123 | if ( $author == false || $author_meta == false ) { |
| 124 | wp_send_json_error(); |
| 125 | die(); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | $author_info['ID'] = $author->ID; |
| 130 | $author_info['display_name'] = $author->display_name; |
| 131 | $author_info['user_url'] = $author->user_url; |
| 132 | $author_info['avatar'] = get_avatar_url( $author_id, array( 'size' => 264 ) ); |
| 133 | $author_info['description'] = get_the_author_meta( 'description', $author_id ); |
| 134 | $author_info['description'] = wpautop( $author_info['description'] ); |
| 135 | $author_info['sabox_social_links'] = Simple_Author_Box_Helper::get_user_social_links( $author_id ); |
| 136 | $author_info['sabox-profile-image'] = get_the_author_meta( 'sabox-profile-image', $author_id ); |
| 137 | |
| 138 | echo json_encode( (object) $author_info ); |
| 139 | die(); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | public function render_sab_block( $atts ) { |
| 144 | return do_shortcode( '[simple-author-box ids=' . $atts['authorID'] . ' ]' ); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | } |
| 149 | |
| 150 | new Simple_Author_Box_Block(); |
| 151 | |
| 152 | |
| 153 |