| 1 |
<?php |
| 2 |
namespace Elementor\Core\Frontend\RenderModes; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use Elementor\Core\Base\Document; |
| 6 |
use Elementor\Core\Frontend\Render_Mode_Manager; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
abstract class Render_Mode_Base { |
| 13 |
const ENQUEUE_SCRIPTS_PRIORITY = 10; |
| 14 |
const ENQUEUE_STYLES_PRIORITY = 10; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
protected $post_id; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var Document |
| 23 |
*/ |
| 24 |
protected $document; |
| 25 |
|
| 26 |
/** |
| 27 |
* Render_Mode_Base constructor. |
| 28 |
* |
| 29 |
* @param $post_id |
| 30 |
*/ |
| 31 |
public function __construct( $post_id ) { |
| 32 |
$this->post_id = intval( $post_id ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns the key name of the class. |
| 37 |
* |
| 38 |
* @return string |
| 39 |
* @throws \Exception |
| 40 |
*/ |
| 41 |
public static function get_name() { |
| 42 |
throw new \Exception( 'You must implements `get_name` static method in ' . static::class ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @param $post_id |
| 47 |
* |
| 48 |
* @return string |
| 49 |
* @throws \Exception |
| 50 |
*/ |
| 51 |
public static function get_url( $post_id ) { |
| 52 |
return Render_Mode_Manager::get_base_url( $post_id, static::get_name() ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Runs before the render, by default load scripts and styles. |
| 57 |
*/ |
| 58 |
public function prepare_render() { |
| 59 |
add_action( 'wp_enqueue_scripts', function () { |
| 60 |
$this->enqueue_scripts(); |
| 61 |
}, static::ENQUEUE_SCRIPTS_PRIORITY ); |
| 62 |
|
| 63 |
add_action( 'wp_enqueue_scripts', function () { |
| 64 |
$this->enqueue_styles(); |
| 65 |
}, static::ENQUEUE_STYLES_PRIORITY ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* By default do not do anything. |
| 70 |
*/ |
| 71 |
protected function enqueue_scripts() { |
| 72 |
// |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* By default do not do anything. |
| 77 |
*/ |
| 78 |
protected function enqueue_styles() { |
| 79 |
// |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Check if the current user has permissions for the current render mode. |
| 84 |
* |
| 85 |
* @return bool |
| 86 |
*/ |
| 87 |
public function get_permissions_callback() { |
| 88 |
return $this->get_document()->is_editable_by_current_user(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Checks if the current render mode is static render, By default returns false. |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public function is_static() { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @return Document |
| 102 |
*/ |
| 103 |
public function get_document() { |
| 104 |
if ( ! $this->document ) { |
| 105 |
$this->document = Plugin::$instance->documents->get( $this->post_id ); |
| 106 |
} |
| 107 |
|
| 108 |
return $this->document; |
| 109 |
} |
| 110 |
} |
| 111 |
|