| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadpages; |
| 4 |
|
| 5 |
use Leadpages\providers\Utils; |
| 6 |
use Leadpages\rest\Service; |
| 7 |
use Leadpages\models\Page; |
| 8 |
use Leadpages\providers\config\Config; |
| 9 |
use Leadpages\models\Options; |
| 10 |
|
| 11 |
defined('ABSPATH') || die('No script kiddies please!'); // Avoid direct file request |
| 12 |
|
| 13 |
/** |
| 14 |
* This is the core class of the plugin responsible for initializing all other dependencies |
| 15 |
* and running the plugin. All of the hooks the plugin depends on are added in this class. |
| 16 |
*/ |
| 17 |
class Core { |
| 18 |
|
| 19 |
use Utils; |
| 20 |
|
| 21 |
/** @var Core $me the singleton instantiation of the plugin */ |
| 22 |
private static $me; |
| 23 |
|
| 24 |
/** @var Activator $activator */ |
| 25 |
private $activator; |
| 26 |
|
| 27 |
/** @var Assets $assets */ |
| 28 |
private $assets; |
| 29 |
|
| 30 |
/** @var Service $service */ |
| 31 |
private $service; |
| 32 |
|
| 33 |
/** @var Proxy $proxy */ |
| 34 |
private $proxy; |
| 35 |
|
| 36 |
/** @var Config */ |
| 37 |
private $config; |
| 38 |
|
| 39 |
/** |
| 40 |
* Instantiate all of the class dependencies. |
| 41 |
* |
| 42 |
* The constructor is protected because a factory method should only create |
| 43 |
* a Core object. |
| 44 |
*/ |
| 45 |
protected function __construct() { |
| 46 |
$this->config = Config::get_instance(); |
| 47 |
|
| 48 |
$this->activator = new Activator(); |
| 49 |
$this->assets = new Assets(); |
| 50 |
$this->service = new Service(); |
| 51 |
$this->proxy = new Proxy(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get singleton core class. |
| 56 |
* |
| 57 |
* @return Core |
| 58 |
*/ |
| 59 |
public static function get_instance() { |
| 60 |
return ! isset(self::$me) ? ( self::$me = new Core() ) : self::$me; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register all of the hooks for the plugin. |
| 65 |
*/ |
| 66 |
public function run() { |
| 67 |
add_action('init', [ $this->get_proxy(), 'serve_landing_page' ], 1); |
| 68 |
add_action('init', [ $this, 'init' ]); |
| 69 |
add_action('rest_api_init', [ $this->get_service(), 'rest_api_init' ]); |
| 70 |
|
| 71 |
add_action('plugins_loaded', [ $this->get_activator(), 'update_db_check' ]); |
| 72 |
register_deactivation_hook(LEADPAGES_FILE, [ $this->get_activator(), 'deactivate' ]); |
| 73 |
|
| 74 |
add_filter('wp_insert_post_data', [ $this, 'check_and_modify_post_slug' ], 1, 1); |
| 75 |
$this->setup_admin_notices(); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* This method is fired on the WordPress init hook. It sets up the admin |
| 80 |
* menu items and enqueues the scripts we need for our admin pages to work. |
| 81 |
*/ |
| 82 |
public function init() { |
| 83 |
add_action('admin_menu', [ $this->get_assets(), 'render_admin_menu_page' ]); |
| 84 |
add_action('admin_menu', [ $this->get_assets(), 'render_oauth_complete_page' ]); |
| 85 |
add_action('admin_enqueue_scripts', [ $this->get_assets(), 'enqueue_admin_scripts' ]); |
| 86 |
|
| 87 |
if ($this->is_user_logged_into_plugin()) { |
| 88 |
add_action('admin_menu', [ $this->get_assets(), 'render_settings_page' ]); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Alert the user if: |
| 94 |
* - Permalinks are not enabled (i.e. "Plain") |
| 95 |
*/ |
| 96 |
private function setup_admin_notices() { |
| 97 |
if ('' === Options::get(Options::$permalink_structure)) { |
| 98 |
add_action('admin_notices', [ $this, 'turn_on_permalinks' ]); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Show an admin notice informing the user that they need to enable permalinks |
| 105 |
*/ |
| 106 |
public function turn_on_permalinks() { |
| 107 |
echo wp_kses( |
| 108 |
"<div class='notice notice-error is-dismissible'> |
| 109 |
<p> Leadpages plugin needs |
| 110 |
<a href='options-permalink.php'>permalinks</a> enabled! |
| 111 |
Permalink structure can not be 'Plain'. |
| 112 |
</p></div>", |
| 113 |
[ |
| 114 |
'div' => [ |
| 115 |
'class' => [], |
| 116 |
], |
| 117 |
'p' => [], |
| 118 |
'a' => [ |
| 119 |
'href' => [], |
| 120 |
], |
| 121 |
] |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Check if Leadpages landing page is already connected with the same slug as the |
| 127 |
* name of the WordPress post. This would cause permalink conflicts when serving pages if not prevented. |
| 128 |
* Update the post name to a unique value across the posts and Leadpages tables. |
| 129 |
* |
| 130 |
* @param array $data an array of slashed, sanitized, and processed wp post data |
| 131 |
* @return array |
| 132 |
*/ |
| 133 |
public function check_and_modify_post_slug( $data ) { |
| 134 |
$slug = $data['post_name']; |
| 135 |
$conflicts = Page::get_by_slug($slug); |
| 136 |
|
| 137 |
// if a conflict is found, modify the slug by adding a numbered suffix and repeat until there is no conflict |
| 138 |
if ($conflicts) { |
| 139 |
$suffix = 2; |
| 140 |
do { |
| 141 |
$new_slug = $slug . '-' . $suffix; |
| 142 |
++$suffix; |
| 143 |
$this->debug("Conflict with $slug, changing post name to $new_slug"); |
| 144 |
$conflicts = Page::get_by_slug($new_slug); |
| 145 |
} while ($conflicts); |
| 146 |
|
| 147 |
$data['post_name'] = $new_slug; |
| 148 |
} |
| 149 |
|
| 150 |
return $data; |
| 151 |
} |
| 152 |
|
| 153 |
/** @return Activator */ |
| 154 |
public function get_activator() { |
| 155 |
return $this->activator; |
| 156 |
} |
| 157 |
|
| 158 |
/** @return Assets */ |
| 159 |
public function get_assets() { |
| 160 |
return $this->assets; |
| 161 |
} |
| 162 |
|
| 163 |
/** @return Service */ |
| 164 |
public function get_service() { |
| 165 |
return $this->service; |
| 166 |
} |
| 167 |
|
| 168 |
/** @return Proxy */ |
| 169 |
public function get_proxy() { |
| 170 |
return $this->proxy; |
| 171 |
} |
| 172 |
} |
| 173 |
|