class-simple-author-box.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /**
  3. * Our main plugin class
  4. */
  5. class Simple_Author_Box {
  6. private static $instance = null;
  7. private $options;
  8. /**
  9. * Function constructor
  10. */
  11. function __construct() {
  12. $this->load_dependencies();
  13. $this->define_admin_hooks();
  14. add_action( 'init', array( $this, 'define_public_hooks' ) );
  15. }
  16. /**
  17. * Singleton pattern
  18. *
  19. * @return void
  20. */
  21. public static function get_instance() {
  22. if ( is_null( self::$instance ) ) {
  23. self::$instance = new self();
  24. }
  25. return self::$instance;
  26. }
  27. private function load_dependencies() {
  28. require_once SIMPLE_AUTHOR_BOX_PATH . 'inc/class-simple-author-box-social.php';
  29. require_once SIMPLE_AUTHOR_BOX_PATH . 'inc/class-simple-author-box-helper.php';
  30. require_once SIMPLE_AUTHOR_BOX_PATH . 'inc/functions.php';
  31. // everything below this line gets loaded only in the admin back-end
  32. if ( is_admin() ) {
  33. require_once SIMPLE_AUTHOR_BOX_PATH . 'inc/class-simple-author-box-admin-page.php';
  34. require_once SIMPLE_AUTHOR_BOX_PATH . 'inc/class-simple-author-box-user-profile.php';
  35. require_once SIMPLE_AUTHOR_BOX_PATH . 'inc/class-simple-author-box-previewer.php';
  36. }
  37. }
  38. /**
  39. * Admin hooks
  40. *
  41. * @return void
  42. */
  43. private function define_admin_hooks() {
  44. /**
  45. * everything hooked here loads on both front-end & back-end
  46. */
  47. add_filter( 'get_avatar', array( $this, 'replace_gravatar_image' ), 10, 6 );
  48. add_filter( 'amp_post_template_data', array( $this, 'sab_amp_css' ) ); // @since 2.0.7
  49. /**
  50. * Only load when we're in the admin panel
  51. */
  52. if ( is_admin() ) {
  53. add_action( 'init', array( $this, 'initialize_admin' ) );
  54. add_action( 'admin_enqueue_scripts', array( $this, 'admin_style_and_scripts' ) );
  55. add_filter( 'user_contactmethods', array( $this, 'add_extra_fields' ) );
  56. add_filter( 'plugin_action_links_' . SIMPLE_AUTHOR_BOX_SLUG, array( $this, 'settings_link' ) );
  57. }
  58. }
  59. public function initialize_admin() {
  60. // Class that handles admin page
  61. new Simple_Author_Box_Admin_Page();
  62. // Class that handles author box previewer
  63. new Simple_Author_Box_Previewer();
  64. }
  65. /**
  66. * See this: https://codex.wordpress.org/Plugin_API/Filter_Reference/get_avatar
  67. *
  68. * Custom function to overwrite WordPress's get_avatar function
  69. *
  70. * @param [type] $avatar
  71. * @param [type] $id_or_email
  72. * @param [type] $size
  73. * @param [type] $default
  74. * @param [type] $alt
  75. * @param [type] $args
  76. * @return void
  77. */
  78. public function replace_gravatar_image( $avatar, $id_or_email, $size, $default, $alt, $args = array() ) {
  79. // Process the user identifier.
  80. $user = false;
  81. if ( is_numeric( $id_or_email ) ) {
  82. $user = get_user_by( 'id', absint( $id_or_email ) );
  83. } elseif ( is_string( $id_or_email ) ) {
  84. $user = get_user_by( 'email', $id_or_email );
  85. } elseif ( $id_or_email instanceof WP_User ) {
  86. // User Object
  87. $user = $id_or_email;
  88. } elseif ( $id_or_email instanceof WP_Post ) {
  89. // Post Object
  90. $user = get_user_by( 'id', (int) $id_or_email->post_author );
  91. } elseif ( $id_or_email instanceof WP_Comment ) {
  92. if ( ! empty( $id_or_email->user_id ) ) {
  93. $user = get_user_by( 'id', (int) $id_or_email->user_id );
  94. }
  95. }
  96. if ( ! $user || is_wp_error( $user ) ) {
  97. return $avatar;
  98. }
  99. $custom_profile_image = get_user_meta( $user->ID, 'sabox-profile-image', true );
  100. $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
  101. if ( ! $args['found_avatar'] || $args['force_default'] ) {
  102. $class[] = 'avatar-default';
  103. }
  104. if ( $args['class'] ) {
  105. if ( is_array( $args['class'] ) ) {
  106. $class = array_merge( $class, $args['class'] );
  107. } else {
  108. $class[] = $args['class'];
  109. }
  110. }
  111. $class[] = 'sab-custom-avatar';
  112. if ( '' !== $custom_profile_image && true !== $args['force_default'] ) {
  113. $avatar = sprintf(
  114. "<img alt='%s' src='%s' srcset='%s' class='%s' %s/>",
  115. esc_attr( $args['alt'] ),
  116. esc_url( $custom_profile_image ),
  117. esc_url( $custom_profile_image ) . ' 2x',
  118. esc_attr( join( ' ', $class ) ),
  119. $args['extra_attr']
  120. );
  121. }
  122. return $avatar;
  123. }
  124. public function define_public_hooks() {
  125. $this->options = Simple_Author_Box_Helper::get_option( 'saboxplugin_options' );
  126. $this->options['sab_footer_inline_style'] = Simple_Author_Box_Helper::get_option( 'sab_footer_inline_style' );
  127. add_action( 'wp_enqueue_scripts', array( $this, 'saboxplugin_author_box_style' ), 10 );
  128. add_shortcode( 'simple-author-box', array( $this, 'shortcode' ) );
  129. add_filter( 'sabox_hide_social_icons', array( $this, 'show_social_media_icons' ), 10, 2 );
  130. if ( '0' == $this->options['sab_autoinsert'] ) {
  131. add_filter( 'the_content', 'wpsabox_author_box' );
  132. }
  133. if ( '0' == $this->options['sab_footer_inline_style'] ) {
  134. add_action(
  135. 'wp_footer', array(
  136. $this,
  137. 'inline_style',
  138. ), 13
  139. );
  140. } else {
  141. add_action( 'wp_head', array( $this, 'inline_style' ), 15 );
  142. }
  143. }
  144. public function settings_link( array $links ) {
  145. $links['sab'] = sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=simple-author-box-options' ), __( 'Settings', 'saboxplugin' ) );
  146. return $links;
  147. }
  148. public function admin_style_and_scripts( $hook ) {
  149. $suffix = '.min';
  150. if ( SIMPLE_AUTHOR_SCRIPT_DEBUG ) {
  151. $suffix = '';
  152. }
  153. // globally loaded
  154. wp_enqueue_style( 'sabox-css', SIMPLE_AUTHOR_BOX_ASSETS . 'css/sabox.css' );
  155. wp_enqueue_style( 'saboxplugin-admin-style', SIMPLE_AUTHOR_BOX_ASSETS . 'css/sabox-admin-style' . $suffix . '.css' );
  156. // loaded only on plugin page
  157. if ( 'toplevel_page_simple-author-box-options' == $hook ) {
  158. // Styles
  159. wp_enqueue_style( 'wp-color-picker' );
  160. wp_enqueue_style( 'jquery-ui', SIMPLE_AUTHOR_BOX_ASSETS . 'css/jquery-ui.min.css' );
  161. // Scripts
  162. wp_enqueue_script(
  163. 'sabox-admin-js', SIMPLE_AUTHOR_BOX_ASSETS . 'js/sabox-admin.js', array(
  164. 'jquery-ui-slider',
  165. 'wp-color-picker',
  166. ), false, true
  167. );
  168. wp_enqueue_script(
  169. 'sabox-plugin-install', SIMPLE_AUTHOR_BOX_ASSETS . 'js/plugin-install.js', array(
  170. 'jquery',
  171. 'updates',
  172. ), '1.0.0', 'all'
  173. );
  174. // loaded only on user profile page
  175. } elseif ( 'profile.php' == $hook || 'user-edit.php' == $hook ) {
  176. wp_enqueue_style( 'saboxplugin-admin-style', SIMPLE_AUTHOR_BOX_ASSETS . 'css/sabox-admin-style' . $suffix . '.css' );
  177. wp_enqueue_media();
  178. wp_enqueue_editor();
  179. wp_enqueue_script( 'sabox-admin-editor-js', SIMPLE_AUTHOR_BOX_ASSETS . 'js/sabox-editor.js', array(), false, true );
  180. $sabox_js_helper = array();
  181. $social_icons = apply_filters( 'sabox_social_icons', Simple_Author_Box_Helper::$social_icons );
  182. unset( $social_icons['user_email'] );
  183. $sabox_js_helper['socialIcons'] = $social_icons;
  184. wp_localize_script( 'sabox-admin-editor-js', 'SABHerlper', $sabox_js_helper );
  185. }
  186. }
  187. public function add_extra_fields( $extra_fields ) {
  188. unset( $extra_fields['aim'] );
  189. unset( $extra_fields['jabber'] );
  190. unset( $extra_fields['yim'] );
  191. return $extra_fields;
  192. }
  193. /*----------------------------------------------------------------------------------------------------------
  194. Adding the author box main CSS
  195. -----------------------------------------------------------------------------------------------------------*/
  196. public function saboxplugin_author_box_style() {
  197. $suffix = '.min';
  198. if ( SIMPLE_AUTHOR_SCRIPT_DEBUG ) {
  199. $suffix = '';
  200. }
  201. $sab_protocol = is_ssl() ? 'https' : 'http';
  202. $sab_box_subset = Simple_Author_Box_Helper::get_option( 'sab_box_subset' );
  203. /**
  204. * Check for duplicate font families, remove duplicates & re-work the font enqueue procedure
  205. *
  206. * @since 2.0.4
  207. */
  208. if ( 'none' != strtolower( $sab_box_subset ) ) {
  209. $sab_subset = '&amp;subset=' . strtolower( $sab_box_subset );
  210. } else {
  211. $sab_subset = '&amp;subset=latin';
  212. }
  213. $sab_author_font = Simple_Author_Box_Helper::get_option( 'sab_box_name_font' );
  214. $sab_desc_font = Simple_Author_Box_Helper::get_option( 'sab_box_desc_font' );
  215. $sab_web_font = Simple_Author_Box_Helper::get_option( 'sab_box_web_font' );
  216. $google_fonts = array();
  217. if ( $sab_author_font && 'none' != strtolower( $sab_author_font ) ) {
  218. $google_fonts[] = str_replace( ' ', '+', esc_attr( $sab_author_font ) );
  219. }
  220. if ( $sab_desc_font && 'none' != strtolower( $sab_desc_font ) ) {
  221. $google_fonts[] = str_replace( ' ', '+', esc_attr( $sab_desc_font ) );
  222. }
  223. if ( '1' == $this->options['sab_web'] && $sab_web_font && 'none' != strtolower( $sab_web_font ) ) {
  224. $google_fonts[] = str_replace( ' ', '+', esc_attr( $sab_web_font ) );
  225. }
  226. $google_fonts = apply_filters( 'sabox_google_fonts', $google_fonts );
  227. $google_fonts = array_unique( $google_fonts );
  228. if ( ! empty( $google_fonts ) ) { // let's check the array's not empty before actually loading; we want to avoid loading 'none' font-familes
  229. $final_google_fonts = array();
  230. foreach ( $google_fonts as $v ) {
  231. $final_google_fonts[] = $v . ':400,700,400italic,700italic';
  232. }
  233. wp_register_style( 'sab-font', $sab_protocol . '://fonts.googleapis.com/css?family=' . implode( '|', $final_google_fonts ) . $sab_subset, array(), null );
  234. }
  235. /**
  236. * end changes introduced in 2.0.4
  237. */
  238. if ( ! is_single() and ! is_page() and ! is_author() and ! is_archive() ) {
  239. return;
  240. }
  241. if ( ! empty( $google_fonts ) ) {
  242. wp_enqueue_style( 'sab-font' );
  243. }
  244. }
  245. public function inline_style() {
  246. if ( ! is_single() and ! is_page() and ! is_author() and ! is_archive() ) {
  247. return;
  248. }
  249. $style = '<style type="text/css">';
  250. $style .= Simple_Author_Box_Helper::generate_inline_css();
  251. $style .= '</style>';
  252. echo $style;
  253. }
  254. public function shortcode( $atts ) {
  255. $defaults = array(
  256. 'ids' => '',
  257. );
  258. $atts = wp_parse_args( $atts, $defaults );
  259. if ( '' != $atts['ids'] ) {
  260. $ids = explode( ',', $atts['ids'] );
  261. ob_start();
  262. $sabox_options = Simple_Author_Box_Helper::get_option( 'saboxplugin_options' );
  263. foreach ( $ids as $user_id ) {
  264. $template = Simple_Author_Box_Helper::get_template();
  265. $sabox_author_id = $user_id;
  266. echo '<div class="sabox-plus-item">';
  267. include( $template );
  268. echo '</div>';
  269. }
  270. $html = ob_get_clean();
  271. } else {
  272. $html = wpsabox_author_box();
  273. }
  274. return $html;
  275. }
  276. public function show_social_media_icons( $return, $user ) {
  277. if ( in_array( 'sab-guest-author', (array) $user->roles ) ) {
  278. return false;
  279. }
  280. return true;
  281. }
  282. /**
  283. * AMP compatibility
  284. * @since 2.0
  285. *
  286. * @param $data
  287. *
  288. * @return mixed
  289. */
  290. function sab_amp_css( $data ) {
  291. $options = Simple_Author_Box_Helper::get_option( 'saboxplugin_options' );
  292. $icon_size = absint( Simple_Author_Box_Helper::get_option( 'sab_box_icon_size' ) );
  293. if ( '1' == $options['sab_colored'] ) {
  294. $icon_size = $icon_size * 2;
  295. }
  296. $data['post_amp_styles'] = array(
  297. '.saboxplugin-wrap .saboxplugin-gravatar' => array(
  298. 'float: left',
  299. 'padding: 20px',
  300. ),
  301. '.saboxplugin-wrap .saboxplugin-gravatar img' => array(
  302. 'max-width: 100px',
  303. 'height: auto',
  304. ),
  305. '.saboxplugin-wrap .saboxplugin-authorname' => array(
  306. 'font-size: 18px',
  307. 'line-height: 1',
  308. 'margin: 20px 0 0 20px',
  309. 'display: block',
  310. ),
  311. '.saboxplugin-wrap .saboxplugin-authorname a' => array(
  312. 'text-decoration: none',
  313. ),
  314. '.saboxplugin-wrap .saboxplugin-desc' => array(
  315. 'display: block',
  316. 'margin: 5px 20px',
  317. ),
  318. '.saboxplugin-wrap .saboxplugin-desc a' => array(
  319. 'text-decoration: none',
  320. ),
  321. '.saboxplugin-wrap .saboxplugin-desc p' => array(
  322. 'margin: 5px 0 12px 0',
  323. 'font-size: ' . absint( Simple_Author_Box_Helper::get_option( 'sab_box_desc_size' ) ) . 'px',
  324. 'line-height: ' . absint( Simple_Author_Box_Helper::get_option( 'sab_box_desc_size' ) + 7 ) . 'px',
  325. ),
  326. '.saboxplugin-wrap .saboxplugin-web' => array(
  327. 'margin: 0 20px 15px',
  328. 'text-align: left',
  329. ),
  330. '.saboxplugin-wrap .saboxplugin-socials' => array(
  331. 'position: relative',
  332. 'display: block',
  333. 'background: #fcfcfc',
  334. 'padding: 5px',
  335. 'border-top: 1px solid #eee;',
  336. ),
  337. '.saboxplugin-wrap .saboxplugin-socials a' => array(
  338. 'text-decoration: none',
  339. 'box-shadow: none',
  340. 'padding: 0',
  341. 'margin: 0',
  342. 'border: 0',
  343. 'transition: opacity 0.4s',
  344. '-webkit-transition: opacity 0.4s',
  345. '-moz-transition: opacity 0.4s',
  346. '-o-transition: opacity 0.4s',
  347. 'display: inline-block',
  348. ),
  349. '.saboxplugin-wrap .saboxplugin-socials .saboxplugin-icon-grey' => array(
  350. 'display: inline-block',
  351. 'vertical-align: middle',
  352. 'margin: 10px 5px',
  353. 'color: #444',
  354. ),
  355. '.saboxplugin-wrap .saboxplugin-socials a svg' => array(
  356. 'width:' . absint( $icon_size ) . 'px;',
  357. 'height:' . absint( $icon_size ) . 'px',
  358. 'display:block'
  359. ),
  360. '.saboxplugin-wrap .saboxplugin-socials.sabox-colored .saboxplugin-icon-color' => array(
  361. 'color: #FFF',
  362. 'margin: 5px',
  363. 'vertical-align: middle',
  364. 'display: inline-block',
  365. ),
  366. '.saboxplugin-wrap .clearfix' => array(
  367. 'clear:both;',
  368. ),
  369. '.saboxplugin-wrap .saboxplugin-socials a svg .st2' => array(
  370. 'fill: #fff;'
  371. ),
  372. '.saboxplugin-wrap .saboxplugin-socials a svg .st1' => array(
  373. 'fill: rgba( 0, 0, 0, .3 );'
  374. ),
  375. // custom paddings & margins
  376. '.saboxplugin-wrap' => array(
  377. 'margin-top: ' . absint( Simple_Author_Box_Helper::get_option( 'sab_box_margin_top' ) ) . 'px',
  378. 'margin-bottom: ' . absint( Simple_Author_Box_Helper::get_option( 'sab_box_margin_bottom' ) ) . 'px',
  379. 'padding: ' . absint( Simple_Author_Box_Helper::get_option( 'sab_box_padding_top_bottom' ) ) . 'px ' . absint( Simple_Author_Box_Helper::get_option( 'sab_box_padding_left_right' ) ) . 'px',
  380. 'box-sizing: border-box',
  381. 'border: 1px solid #EEE',
  382. 'width: 100%',
  383. 'clear: both',
  384. 'overflow : hidden',
  385. 'word-wrap: break-word',
  386. 'position: relative',
  387. ),
  388. '.sab-edit-settings' => array(
  389. 'display: none;',
  390. ),
  391. '.sab-profile-edit' => array(
  392. 'display: none;',
  393. ),
  394. );
  395. if ( '1' == $options['sab_colored'] && '1' != $options['sab_box_long_shadow'] ) {
  396. $data['post_amp_styles']['.saboxplugin-wrap .saboxplugin-socials .saboxplugin-icon-color .st1'] = array(
  397. 'display: none;',
  398. );
  399. }
  400. return $data;
  401. }
  402. }