diff --git a/filter/mediaplugin/filter.php b/filter/mediaplugin/filter.php index e98f6d9d16e..726c1d1abd8 100644 --- a/filter/mediaplugin/filter.php +++ b/filter/mediaplugin/filter.php @@ -57,8 +57,8 @@ class filter_mediaplugin extends moodle_text_filter { } $jsinitialised = true; - $mediamanager = core_media_manager::instance(); - $mediamanager->setup($page); + // Set up the media manager so that media plugins requiring JS are initialised. + $mediamanager = core_media_manager::instance($page); } public function filter($text, array $options = array()) { diff --git a/lib/editor/tinymce/plugins/moodlemedia/preview.php b/lib/editor/tinymce/plugins/moodlemedia/preview.php index af946c04c11..cf4802fd56e 100644 --- a/lib/editor/tinymce/plugins/moodlemedia/preview.php +++ b/lib/editor/tinymce/plugins/moodlemedia/preview.php @@ -40,7 +40,7 @@ $PAGE->add_body_class('core_media_preview'); echo $OUTPUT->header(); -$mediarenderer = core_media_manager::instance(); +$mediarenderer = core_media_manager::instance($PAGE); if (isloggedin() and !isguestuser() and $mediarenderer->can_embed_url($url)) { require_sesskey(); diff --git a/lib/tests/medialib_test.php b/lib/tests/medialib_test.php index d2ccdc2a9dd..1710863eaf7 100644 --- a/lib/tests/medialib_test.php +++ b/lib/tests/medialib_test.php @@ -517,4 +517,11 @@ class core_media_manager_test extends core_media_manager { } return $out; } + + /** + * Override the constructor to access it. + */ + public function __construct() { + parent::__construct(); + } } diff --git a/media/classes/manager.php b/media/classes/manager.php index 84b4adb9fe9..d7a1b6e3251 100644 --- a/media/classes/manager.php +++ b/media/classes/manager.php @@ -100,27 +100,52 @@ class core_media_manager { /** * Returns a singleton instance of a manager * + * Note as of Moodle 3.2.2, this will call setup for you. + * * @return core_media_manager */ - public static function instance() { + public static function instance($page = null) { if (self::$instance === null) { - self::$instance = new self(); + self::$instance = new self($page); } return self::$instance; } + /** + * Construct a new core_media_manager instance + * + * @param moodle_page $page The page we are going to add requirements to. + * @see core_media_manager::instance() + */ + protected function __construct($page = null) { + // Use the passed $page if given, otherwise the $PAGE global. + if ($page == null) { + global $PAGE; + if (isset($PAGE)) { + $page = $PAGE; + } + } + if ($page) { + $players = $this->get_players(); + foreach ($players as $player) { + $player->setup($page); + } + } else { + debugging('Could not determine the $PAGE. Media plugins will not be set up', DEBUG_DEVELOPER); + } + } + /** * Setup page requirements. * * This should must only be called once per page request. * + * This function will be deprecated in Moodle 3.3, The setup is now done in ::instance() so there is no need to call this. * @param moodle_page $page The page we are going to add requirements to. + * @see core_media_manager::instance() */ public function setup($page) { - $players = $this->get_players(); - foreach ($players as $player) { - $player->setup($page); - } + // No need to call ::instance from here, because the instance has already be set up. } /** diff --git a/media/upgrade.txt b/media/upgrade.txt new file mode 100644 index 00000000000..eccdb6ca50a --- /dev/null +++ b/media/upgrade.txt @@ -0,0 +1,5 @@ +This files describes API changes in /media/ plugins, +information provided here is intended especially for developers. + +=== 3.2.2 === +* core_media_manager setup() is now no longer needed as it is now called when initialising core_media_manager::instance(). diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 4e02e441fc5..9a973c7f102 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -618,7 +618,7 @@ function lesson_get_media_html($lesson, $context) { $extension = resourcelib_get_extension($url->out(false)); - $mediamanager = core_media_manager::instance(); + $mediamanager = core_media_manager::instance($PAGE); $embedoptions = array( core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true diff --git a/mod/resource/locallib.php b/mod/resource/locallib.php index 062db7ec179..0dd1f1fe2a3 100644 --- a/mod/resource/locallib.php +++ b/mod/resource/locallib.php @@ -75,7 +75,7 @@ function resource_display_embed($resource, $cm, $course, $file) { $extension = resourcelib_get_extension($file->get_filename()); - $mediamanager = core_media_manager::instance(); + $mediamanager = core_media_manager::instance($PAGE); $embedoptions = array( core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true, diff --git a/mod/url/locallib.php b/mod/url/locallib.php index a29686c7d64..7d9bba291cf 100644 --- a/mod/url/locallib.php +++ b/mod/url/locallib.php @@ -305,7 +305,7 @@ function url_display_embed($url, $cm, $course) { $extension = resourcelib_get_extension($url->externalurl); - $mediamanager = core_media_manager::instance(); + $mediamanager = core_media_manager::instance($PAGE); $embedoptions = array( core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true