|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Add task to install and activate an SEO plugin. |
| 4 | + * |
| 5 | + * @package Progress_Planner |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Progress_Planner\Suggested_Tasks\Providers; |
| 9 | + |
| 10 | +use Progress_Planner\Suggested_Tasks\Data_Collector\SEO_Plugin as SEO_Plugin_Data_Collector; |
| 11 | + |
| 12 | +/** |
| 13 | + * Add task to install and activate an SEO plugin. |
| 14 | + */ |
| 15 | +class SEO_Plugin extends Tasks_Interactive { |
| 16 | + |
| 17 | + /** |
| 18 | + * Whether the task is an onboarding task. |
| 19 | + * |
| 20 | + * @var bool |
| 21 | + */ |
| 22 | + protected const IS_ONBOARDING_TASK = true; |
| 23 | + |
| 24 | + /** |
| 25 | + * The provider ID. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected const PROVIDER_ID = 'seo-plugin'; |
| 30 | + |
| 31 | + /** |
| 32 | + * The popover ID. |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + const POPOVER_ID = 'seo-plugin'; |
| 37 | + |
| 38 | + /** |
| 39 | + * The data collector class name. |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + protected const DATA_COLLECTOR_CLASS = SEO_Plugin_Data_Collector::class; |
| 44 | + |
| 45 | + /** |
| 46 | + * The external link URL. |
| 47 | + * |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + protected const EXTERNAL_LINK_URL = 'https://prpl.fyi/install-seo-plugin'; |
| 51 | + |
| 52 | + /** |
| 53 | + * The priority of the task. |
| 54 | + * |
| 55 | + * @var int |
| 56 | + */ |
| 57 | + protected $priority = self::PRIORITY_HIGH; |
| 58 | + |
| 59 | + /** |
| 60 | + * Get the task URL. |
| 61 | + * |
| 62 | + * @return string |
| 63 | + */ |
| 64 | + protected function get_url() { |
| 65 | + return \admin_url( 'plugins.php' ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the task title. |
| 70 | + * |
| 71 | + * @return string |
| 72 | + */ |
| 73 | + protected function get_title() { |
| 74 | + return \esc_html__( 'Install an SEO plugin', 'progress-planner' ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Check if the task should be added. |
| 79 | + * |
| 80 | + * @return bool |
| 81 | + */ |
| 82 | + public function should_add_task() { |
| 83 | + // Only add the task if no SEO plugin is detected. |
| 84 | + return ! $this->get_data_collector()->collect(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Check if the task is completed. |
| 89 | + * |
| 90 | + * @param string $task_id The task ID. |
| 91 | + * |
| 92 | + * @return bool |
| 93 | + */ |
| 94 | + public function is_task_completed( $task_id = '' ) { |
| 95 | + // Task is completed if an SEO plugin is detected. |
| 96 | + return $this->get_data_collector()->collect(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the popover instructions. |
| 101 | + * |
| 102 | + * @return void |
| 103 | + */ |
| 104 | + public function print_popover_instructions() { |
| 105 | + $seo_plugin_recommendation_slug = \progress_planner()->get_ui__branding()->get_seo_plugin_recommendation_slug(); |
| 106 | + $seo_plugin_recommendation_name = $this->get_plugin_name_by_slug( $seo_plugin_recommendation_slug ); |
| 107 | + if ( ! $seo_plugin_recommendation_name ) { |
| 108 | + return; |
| 109 | + } |
| 110 | + ?> |
| 111 | + <p> |
| 112 | + <?php \esc_html_e( 'Search Engine Optimization (SEO) is essential for improving your website\'s visibility in search engines like Google. An SEO plugin helps you optimize your content, manage meta tags, generate sitemaps, and follow best practices to rank higher in search results.', 'progress-planner' ); ?> |
| 113 | + </p> |
| 114 | + <p> |
| 115 | + <?php |
| 116 | + printf( |
| 117 | + /* translators: %s is the plugin name */ |
| 118 | + \esc_html__( 'We recommend installing %s, one of the most popular and comprehensive SEO plugins for WordPress.', 'progress-planner' ), |
| 119 | + sprintf( |
| 120 | + '<a href="https://w.org/plugins/%1$s/" target="_blank">%2$s</a>', |
| 121 | + \esc_attr( $seo_plugin_recommendation_slug ), |
| 122 | + \esc_html( $seo_plugin_recommendation_name ) |
| 123 | + ) |
| 124 | + ); |
| 125 | + ?> |
| 126 | + </p> |
| 127 | + <?php |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Print the popover input field for the form. |
| 132 | + * |
| 133 | + * @return void |
| 134 | + */ |
| 135 | + public function print_popover_form_contents() { |
| 136 | + ?> |
| 137 | + <?php if ( ! \is_multisite() && \current_user_can( 'install_plugins' ) ) : ?> |
| 138 | + <prpl-install-plugin |
| 139 | + data-plugin-name="Yoast SEO" |
| 140 | + data-plugin-slug="wordpress-seo" |
| 141 | + data-action="<?php echo \progress_planner()->get_plugin_installer()->is_plugin_installed( 'wordpress-seo' ) ? 'activate' : 'install'; ?>" |
| 142 | + data-provider-id="<?php echo \esc_attr( self::PROVIDER_ID ); ?>" |
| 143 | + ></prpl-install-plugin> |
| 144 | + <?php else : ?> |
| 145 | + <p> |
| 146 | + <?php |
| 147 | + if ( \is_multisite() ) { |
| 148 | + \esc_html_e( 'Plugin installation is disabled on this multisite installation.', 'progress-planner' ); |
| 149 | + } else { |
| 150 | + \esc_html_e( 'You do not have permission to install plugins.', 'progress-planner' ); |
| 151 | + } |
| 152 | + ?> |
| 153 | + </p> |
| 154 | + <p> |
| 155 | + <a href="<?php echo \esc_url( \admin_url( 'plugins.php' ) ); ?>" class="prpl-button prpl-button-primary"> |
| 156 | + <?php \esc_html_e( 'Go to Plugins', 'progress-planner' ); ?> |
| 157 | + </a> |
| 158 | + </p> |
| 159 | + <?php endif; ?> |
| 160 | + <?php |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Get plugin name from the data collector by slug. |
| 165 | + * |
| 166 | + * @param string $slug The plugin slug. |
| 167 | + * |
| 168 | + * @return string|null Plugin name or null if not found. |
| 169 | + */ |
| 170 | + public function get_plugin_name_by_slug( $slug ) { |
| 171 | + $data_collector = $this->get_data_collector(); |
| 172 | + if ( ! $data_collector instanceof SEO_Plugin_Data_Collector ) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + $seo_plugins = $data_collector->get_seo_plugins(); |
| 177 | + if ( ! isset( $seo_plugins[ $slug ]['name'] ) ) { |
| 178 | + return null; |
| 179 | + } |
| 180 | + |
| 181 | + return $seo_plugins[ $slug ]['name']; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Add task actions specific to this task. |
| 186 | + * |
| 187 | + * @param array $data The task data. |
| 188 | + * @param array $actions The existing actions. |
| 189 | + * |
| 190 | + * @return array |
| 191 | + */ |
| 192 | + public function add_task_actions( $data = [], $actions = [] ) { |
| 193 | + $actions[] = [ |
| 194 | + 'priority' => 10, |
| 195 | + 'html' => '<a href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover()">' . \esc_html__( 'Install plugin', 'progress-planner' ) . '</a>', |
| 196 | + ]; |
| 197 | + |
| 198 | + return $actions; |
| 199 | + } |
| 200 | +} |
0 commit comments