private function _wrappedParse($content) { $keyword = $this->_context->get(tubepress_api_options_Names::SHORTCODE_KEYWORD); if (!$this->somethingToParse($content, $keyword)) { return; } preg_match("/\\[{$keyword}\\b(.*)\\]/", $content, $matches); if ($this->_shouldLog) { $this->_logger->debug(sprintf('Found a shortcode: %s', $this->_stringUtils->redactSecrets($matches[0]))); } $this->_lastShortcodeUsed = $matches[0]; /* Anything matched? */ if (isset($matches[1]) && $matches[1] != '') { $text = preg_replace('/[\\x{00a0}\\x{200b}]+/u', ' ', $matches[1]); $text = self::_convertQuotes($text); $pattern = '/(\\w+)\\s*=\\s*"([^"]*)"(?:\\s*,)?(?:\\s|$)|(\\w+)\\s*=\\s*\'([^\']*)\'(?:\\s*,)?(?:\\s|$)|(\\w+)\\s*=\\s*([^\\s\'"]+)(?:\\s*,)?(?:\\s|$)/'; if (preg_match_all($pattern, $text, $match, PREG_SET_ORDER)) { if ($this->_shouldLog) { $this->_logger->debug(sprintf('Candidate options detected in shortcode: %s', $this->_stringUtils->redactSecrets($matches[0]))); } $toReturn = $this->_buildNameValuePairArray($match); $this->_context->setEphemeralOptions($toReturn); } } else { if ($this->_shouldLog) { $this->_logger->debug(sprintf('No custom options detected in shortcode: %s', $this->_stringUtils->redactSecrets($matches[0]))); } } }
public function onAjax(tubepress_api_event_EventInterface $ajaxEvent) { $isDebugEnabled = $this->_logger->isEnabled(); if ($isDebugEnabled) { $this->_logger->debug('Handling incoming request. First parsing shortcode.'); } $nvpMap = $this->_requestParams->getParamValue('tubepress_options'); $itemId = $this->_requestParams->getParamValue('tubepress_item'); if ($isDebugEnabled) { $this->_logger->debug('Requested item ID is ' . $itemId); } $this->_context->setEphemeralOptions($nvpMap); if ($this->_context->get(tubepress_api_options_Names::EMBEDDED_LAZYPLAY)) { $this->_context->setEphemeralOption(tubepress_api_options_Names::EMBEDDED_AUTOPLAY, true); } if ($isDebugEnabled) { $this->_logger->debug('Now asking collector for item with ID ' . $itemId); } /* grab the item! */ $mediaItem = $this->_collector->collectSingle($itemId); if ($mediaItem === null) { $this->_responseCode->setResponseCode(404); $ajaxEvent->setArgument('handled', true); return; } if ($isDebugEnabled) { $this->_logger->debug('Collector found item with ID ' . $itemId . '. Sending it to browser'); } $playerHtml = $this->_templating->renderTemplate('gallery/player/ajax', array(tubepress_api_template_VariableNames::MEDIA_ITEM => $mediaItem)); $toReturn = array('mediaItem' => $mediaItem->toHtmlSafeArray(), 'html' => $playerHtml); $this->_responseCode->setResponseCode(200); echo json_encode($toReturn); $ajaxEvent->setArgument('handled', true); }
/** * {@inheritdoc} */ public function collectPage($currentPage) { $modeValueFromContext = $this->_context->get(tubepress_api_options_Names::GALLERY_SOURCE); $originalEphemeralOptions = $this->_context->getEphemeralOptions(); $isPro = $this->_environment->isPro(); if ($isPro) { if ($this->_shouldLog) { $this->_logger->debug('Pro user, leaving collection up to collection listeners.'); } return $this->_collectPage($modeValueFromContext, $currentPage); } /* * Did the user include mode in their ephemeral options? */ if (isset($originalEphemeralOptions[tubepress_api_options_Names::GALLERY_SOURCE])) { if ($this->_shouldLog) { $this->_logger->debug(sprintf('Explicit mode requested in ephemeral options: <code>%s</code>.', $modeValueFromContext)); } return $this->_collectPage($modeValueFromContext, $currentPage); } $jsonEncodedSources = $this->_context->get(tubepress_api_options_Names::SOURCES); if (!$jsonEncodedSources) { if ($this->_shouldLog) { $this->_logger->debug(sprintf('Encoded sources are falsy. Falling back to stored "mode" value of <code>%s</code>.', $modeValueFromContext)); } return $this->_collectPage($modeValueFromContext, $currentPage); } $decodedSources = json_decode($jsonEncodedSources, true); if (!$decodedSources || !is_array($decodedSources) || count($decodedSources) < 1 || !is_array($decodedSources[0])) { if ($this->_shouldLog) { $this->_logger->debug(sprintf('Encoded sources are empty. Falling back to stored "mode" value of <code>%s</code>.', $modeValueFromContext)); } return $this->_collectPage($modeValueFromContext, $currentPage); } if ($this->_shouldLog) { $this->_logger->debug('Popping first stored source and applying it as ephemeral options for collection.'); } $newEphemeralOptions = array_merge($originalEphemeralOptions, $decodedSources[0]); if (!isset($newEphemeralOptions[tubepress_api_options_Names::GALLERY_SOURCE])) { $newEphemeralOptions[tubepress_api_options_Names::GALLERY_SOURCE] = $modeValueFromContext; } $this->_context->setEphemeralOptions($newEphemeralOptions); $page = $this->_collectPage($newEphemeralOptions[tubepress_api_options_Names::GALLERY_SOURCE], $currentPage); if ($this->_shouldLog) { $this->_logger->debug('Restoring saved ephemeral options after page collection.'); } $this->_context->setEphemeralOptions($originalEphemeralOptions); return $page; }
public function onShortcode(tubepress_api_event_EventInterface $incomingEvent) { $subject = $incomingEvent->getSubject(); $rawShortcodeAttributes = $subject[0]; $rawShortcodeContent = isset($subject[1]) ? $subject[1] : ''; if (!is_array($rawShortcodeAttributes)) { $rawShortcodeAttributes = array(); } if ($this->_loggingEnabled) { $this->_logRawShortcode($rawShortcodeAttributes, $rawShortcodeContent); } $normalizedOptions = $this->_normalizeIncomingShortcodeOptionMap($rawShortcodeAttributes); $this->_context->setEphemeralOptions($normalizedOptions); $event = $this->_buildShortcodeEvent($normalizedOptions, $rawShortcodeContent); $this->_eventDispatcher->dispatch(tubepress_wordpress_api_Constants::SHORTCODE_PARSED, $event); /* Get the HTML for this particular shortcode. */ $toReturn = $this->_htmlGenerator->getHtml(); /* reset the context for the next shortcode */ $this->_context->setEphemeralOptions(array()); $incomingEvent->setArgument('result', $toReturn); }
/** * Registers all the styles and scripts for the front end. * * @param array $opts The options. * * @return void */ public function printWidgetHtml(tubepress_api_event_EventInterface $event) { $opts = $event->getSubject(); extract($opts); /* default widget options */ $defaultWidgetOptions = array(tubepress_api_options_Names::FEED_RESULTS_PER_PAGE => 3, tubepress_api_options_Names::META_DISPLAY_VIEWS => false, tubepress_api_options_Names::META_DISPLAY_DESCRIPTION => true, tubepress_api_options_Names::META_DESC_LIMIT => 50, tubepress_api_options_Names::PLAYER_LOCATION => 'shadowbox', tubepress_api_options_Names::GALLERY_THUMB_HEIGHT => 105, tubepress_api_options_Names::GALLERY_THUMB_WIDTH => 135, tubepress_api_options_Names::GALLERY_PAGINATE_ABOVE => false, tubepress_api_options_Names::GALLERY_PAGINATE_BELOW => false, tubepress_api_options_Names::THEME => 'tubepress/default', tubepress_api_options_Names::GALLERY_FLUID_THUMBS => false); /* now apply the user's options */ $rawTag = $this->_context->get(tubepress_wordpress_api_Constants::OPTION_WIDGET_SHORTCODE); $widgetTag = $this->_stringUtils->removeNewLines($rawTag); $this->_shortcodeParser->parse($widgetTag); /* calculate the final options */ $finalOptions = array_merge($defaultWidgetOptions, $this->_context->getEphemeralOptions()); $this->_context->setEphemeralOptions($finalOptions); if ($this->_context->get(tubepress_api_options_Names::THEME) === '') { $this->_context->setEphemeralOption(tubepress_api_options_Names::THEME, 'tubepress/default'); } $out = $this->_htmlGenerator->getHtml(); /* do the standard WordPress widget dance */ /* @noinspection PhpUndefinedVariableInspection */ echo $before_widget . $before_title . $this->_context->get(tubepress_wordpress_api_Constants::OPTION_WIDGET_TITLE) . $after_title . $out . $after_widget; /* reset the context for the next shortcode */ $this->_context->setEphemeralOptions(array()); }