• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / duplicate-post / 14725616456

29 Apr 2025 07:21AM UTC coverage: 45.469% (-4.7%) from 50.122%
14725616456

push

github

web-flow
Merge pull request #402 from Yoast/feature/drop-php-7.2-7.3

Drop support for Php 7.2 and 7.3

1164 of 2560 relevant lines covered (45.47%)

1.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.87
/src/ui/admin-bar.php
1
<?php
2

3
namespace Yoast\WP\Duplicate_Post\UI;
4

5
use WP_Post;
6
use Yoast\WP\Duplicate_Post\Permissions_Helper;
7
use Yoast\WP\Duplicate_Post\Utils;
8

9
/**
10
 * Duplicate Post class to manage the admin bar.
11
 */
12
class Admin_Bar {
13

14
        /**
15
         * Holds the object to create the action link to duplicate.
16
         *
17
         * @var Link_Builder
18
         */
19
        protected $link_builder;
20

21
        /**
22
         * Holds the permissions helper.
23
         *
24
         * @var Permissions_Helper
25
         */
26
        protected $permissions_helper;
27

28
        /**
29
         * Holds the asset manager.
30
         *
31
         * @var Asset_Manager
32
         */
33
        protected $asset_manager;
34

35
        /**
36
         * Initializes the class.
37
         *
38
         * @param Link_Builder       $link_builder       The link builder.
39
         * @param Permissions_Helper $permissions_helper The permissions helper.
40
         * @param Asset_Manager      $asset_manager      The asset manager.
41
         */
42
        public function __construct( Link_Builder $link_builder, Permissions_Helper $permissions_helper, Asset_Manager $asset_manager ) {
2✔
43
                $this->link_builder       = $link_builder;
2✔
44
                $this->permissions_helper = $permissions_helper;
2✔
45
                $this->asset_manager      = $asset_manager;
2✔
46
        }
47

48
        /**
49
         * Adds hooks to integrate with WordPress.
50
         *
51
         * @return void
52
         */
53
        public function register_hooks() {
2✔
54
                if ( \intval( Utils::get_option( 'duplicate_post_show_link_in', 'adminbar' ) ) === 1 ) {
2✔
55
                        \add_action( 'wp_before_admin_bar_render', [ $this, 'admin_bar_render' ] );
2✔
56
                        \add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] );
2✔
57
                        \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] );
2✔
58
                }
59
        }
60

61
        /**
62
         * Shows Rewrite & Republish link in the Toolbar.
63
         *
64
         * @global \WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
65
         *
66
         * @return void
67
         */
68
        public function admin_bar_render() {
8✔
69
                global $wp_admin_bar;
8✔
70

71
                if ( ! \is_admin_bar_showing() ) {
8✔
72
                        return;
2✔
73
                }
74

75
                $post = $this->get_current_post();
6✔
76

77
                if ( ! $post ) {
6✔
78
                        return;
2✔
79
                }
80

81
                $show_new_draft             = ( \intval( Utils::get_option( 'duplicate_post_show_link', 'new_draft' ) ) === 1 );
4✔
82
                $show_rewrite_and_republish = ( \intval( Utils::get_option( 'duplicate_post_show_link', 'rewrite_republish' ) ) === 1 )
4✔
83
                                                                        && $this->permissions_helper->should_rewrite_and_republish_be_allowed( $post );
4✔
84

85
                if ( $show_new_draft && $show_rewrite_and_republish ) {
4✔
86
                        $wp_admin_bar->add_menu(
2✔
87
                                [
2✔
88
                                        'id'    => 'duplicate-post',
2✔
89
                                        'title' => '<span class="ab-icon"></span><span class="ab-label">' . \__( 'Duplicate Post', 'duplicate-post' ) . '</span>',
2✔
90
                                        'href'  => $this->link_builder->build_new_draft_link( $post ),
2✔
91
                                ]
2✔
92
                        );
2✔
93
                        $wp_admin_bar->add_menu(
2✔
94
                                [
2✔
95
                                        'id'     => 'new-draft',
2✔
96
                                        'parent' => 'duplicate-post',
2✔
97
                                        'title'  => \__( 'Copy to a new draft', 'duplicate-post' ),
2✔
98
                                        'href'   => $this->link_builder->build_new_draft_link( $post ),
2✔
99
                                ]
2✔
100
                        );
2✔
101
                        $wp_admin_bar->add_menu(
2✔
102
                                [
2✔
103
                                        'id'     => 'rewrite-republish',
2✔
104
                                        'parent' => 'duplicate-post',
2✔
105
                                        'title'  => \__( 'Rewrite & Republish', 'duplicate-post' ),
2✔
106
                                        'href'   => $this->link_builder->build_rewrite_and_republish_link( $post ),
2✔
107
                                ]
2✔
108
                        );
2✔
109
                }
110
                else {
111
                        if ( $show_new_draft ) {
2✔
112
                                $wp_admin_bar->add_menu(
2✔
113
                                        [
2✔
114
                                                'id'     => 'new-draft',
2✔
115
                                                'title'  => '<span class="ab-icon"></span><span class="ab-label">' . \__( 'Copy to a new draft', 'duplicate-post' ) . '</span>',
2✔
116
                                                'href'   => $this->link_builder->build_new_draft_link( $post ),
2✔
117
                                        ]
2✔
118
                                );
2✔
119
                        }
120

121
                        if ( $show_rewrite_and_republish ) {
2✔
122
                                $wp_admin_bar->add_menu(
×
123
                                        [
×
124
                                                'id'     => 'rewrite-republish',
×
125
                                                'title'  => '<span class="ab-icon"></span><span class="ab-label">' . \__( 'Rewrite & Republish', 'duplicate-post' ) . '</span>',
×
126
                                                'href'   => $this->link_builder->build_rewrite_and_republish_link( $post ),
×
127
                                        ]
×
128
                                );
×
129
                        }
130
                }
131
        }
132

133
        /**
134
         * Links stylesheet for Toolbar link.
135
         *
136
         * @global \WP_Query $wp_the_query.
137
         *
138
         * @return void
139
         */
140
        public function enqueue_styles() {
6✔
141
                if ( ! \is_admin_bar_showing() ) {
6✔
142
                        return;
2✔
143
                }
144

145
                $post = $this->get_current_post();
4✔
146

147
                if ( ! $post ) {
4✔
148
                        return;
2✔
149
                }
150

151
                $this->asset_manager->enqueue_styles();
2✔
152
        }
153

154
        /**
155
         * Returns the current post object (both if it's displayed or being edited).
156
         *
157
         * @global \WP_Query $wp_the_query
158
         *
159
         * @return false|WP_Post The Post object, false if we are not on a post.
160
         */
161
        public function get_current_post() {
10✔
162
                global $wp_the_query;
10✔
163

164
                if ( \is_admin() ) {
10✔
165
                        $post = \get_post();
6✔
166
                }
167
                else {
168
                        $post = $wp_the_query->get_queried_object();
4✔
169
                }
170

171
                if ( empty( $post ) || ! $post instanceof WP_Post ) {
10✔
172
                        return false;
4✔
173
                }
174

175
                if (
176
                        ( ! $this->permissions_helper->is_edit_post_screen() && ! \is_singular( $post->post_type ) )
6✔
177
                        || ! $this->permissions_helper->post_type_has_admin_bar( $post->post_type )
6✔
178
                ) {
179
                        return false;
×
180
                }
181

182
                if ( ! $this->permissions_helper->should_links_be_displayed( $post ) ) {
6✔
183
                        return false;
2✔
184
                }
185

186
                return $post;
4✔
187
        }
188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc