menu = new EllipsisMenu(); $this->menu->label = esc_html__( 'Post Actions', 'p2020' ); $this->menu->classes = 'o2-dropdown-actions o2-post-actions'; // Override Post Actions to use custom html (ellipsis menu). add_filter( 'o2_before_post_actions', array( $this->menu, 'header' ), 11 ); add_filter( 'o2_after_post_actions', array( $this->menu, 'footer' ), 11 ); add_filter( 'o2_filter_post_action_html', array( $this, 'action_item_html' ), 12, 3 ); add_filter( 'o2_filter_post_actions', array( $this, 'filter_xpost_post_actions' ), 14, 2 ); // Add Copy Link (full permalink). add_action( 'init', array( $this, 'permalink_register_post_action_states' ), 10 ); add_filter( 'o2_filter_post_actions', array( $this, 'permalink_add_post_action' ), 10, 2 ); add_filter( 'o2_options', array( $this, 'permalink_update_shortlink_string' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'permalink_enqueue_scripts' ) ); } public function action_item_html( $html, $action, $location = '' ) { if ( false === $html ) { $html = ''; } if ( in_array( $action['action'], [ 'reply', 'login-to-reply', 'follow', 'scrolltocomments' ] ) ) { return ''; } if ( 'resolvedposts' === $action['action'] ) { if ( 'o2-post-footer' !== $location ) { return ''; } return $html; } // Grab the appropriate state settings global $o2_post_action_states; if ( ! is_array( $o2_post_action_states ) ) { // no post action states have been registered with o2_register_post_action_states return $html; } $action_name = $action['action']; if ( ! isset( $o2_post_action_states[ $action_name ] ) ) { return $html; } if ( ! isset( $action['initialState'] ) ) { return $html; } $initial_action_state = $action['initialState']; if ( ! isset( $o2_post_action_states[ $action_name ][ $initial_action_state ] ) ) { return $html; } $initial_state_settings = $o2_post_action_states[ $action_name ][ $initial_action_state ]; $class_array = array_merge( $action['classes'], $initial_state_settings['classes'] ); // classes to be applied to the action (not the post) $url = false === $action['href'] ? '' : $action['href']; $attributes = [ 'title' => $initial_state_settings['title'], 'class' => implode( ' ', $class_array ), 'data-action' => $action_name, 'data-actionstate' => $initial_action_state, ]; if ( isset( $action['rel'] ) && false !== $action['rel'] ) { $attributes['rel'] = $action['rel']; } $html_options = array( 'label' => $initial_state_settings['shortText'], 'url' => $url, 'attributes' => apply_filters( 'p2_post_actions_item_attributes', $attributes, $action ), ); $item_html = $this->menu->generate_item_html( $html_options ); if ( in_array( 'o2-actions-border-bottom', $class_array, true ) ) { $item_html .= $this->menu->generate_item_html( $this->menu::SEPARATOR ); } return $item_html; } public function filter_xpost_post_actions( $actions = false, $post_ID = false ) { if ( false === $actions ) { $actions = array(); } $xpost_url = get_post_meta( $post_ID, '_xpost_original_permalink', true ); if ( $xpost_url ) { $removable_actions = array( 'follow', 'edit', 'scrolltocomments', 'reply', 'stickyposts', 'remembered-posts' ); foreach ( $actions as $key => $item ) { if ( in_array( $item['action'], $removable_actions, true ) ) { unset( $actions[ $key ] ); } } } return $actions; } public function permalink_register_post_action_states() { if ( function_exists( '\o2_register_post_action_states' ) ) { \o2_register_post_action_states( 'permalink', array( 'default' => array( 'shortText' => __( 'Copy link', 'o2' ), 'title' => '', 'classes' => array(), 'genericon' => 'genericon-link', ), ) ); } } public function permalink_add_post_action( $actions = [], $post_ID = false ) { if ( ! $post_ID ) { return $actions; } $post_permalink = is_preview() ? get_preview_post_link( $post_ID ) : get_permalink( $post_ID ); if ( ! $post_permalink ) { return $actions; } $index = 35; // TODO Better index assignment $actions[ $index ] = array( 'action' => 'permalink', 'href' => esc_url( $post_permalink ), 'classes' => array( 'o2-permalink' ), 'initialState' => 'default', ); return $actions; } public function permalink_enqueue_scripts() { wp_enqueue_script( 'copy-permalink-js', get_template_directory_uri() . '/inc/post-actions/js/copy-permalink.js', [ 'o2-cocktail' ], filemtime( __FILE__ ), true ); } /** * Since we are reusing the shortlink event handler, we need to make the success message * a bit generic to fit both shortlink and permalink actions. */ public function permalink_update_shortlink_string( $options ) { $options['strings']['shortlinkCopied'] = __( 'Link copied to clipboard.', 'p2020' ); return $options; } } new PostActions();