public function __construct() { parent::__construct(Config::get('embedly-l4::api_config')); }
/** * Via Embedly. * * @since 151125 OEmbed utilities. * * @param string $string String to parse. */ protected function viaEmbedly(string $string) : string { # Require a valid API key for Embedly. if (!$this->App->Config->©embedly['©api_key']) { throw $this->c::issue('Missing Embedly API key.'); } # Initialize several variables. $tokens = $uncached_urls = $new_embeds = []; $marker = str_replace('.', '', uniqid('', true)); $Embedly = new Embedly(['key' => $this->App->Config->©embedly['©api_key']]); # Tokenize all URLs on a line of their own. $string = preg_replace_callback($this::URL_REGEX_VALID . 'm', function ($m) use(&$tokens, &$uncached_urls, $marker) { $token = '%#%oembed-token-' . $marker . '-' . (count($tokens) - 1) . '%#%'; $tokens[$token] = $uncached_urls[$m[0]] = $m[0]; return $token; // Replaced w/ unique token. }, $string); // Tokenizes all possible embed URLs. # Replace and flag URLs already cached. foreach ($tokens as $_token => $_url) { if (!($_embed = $this->getEmbedlyCache($_url))) { continue; // Not cached yet. } unset($uncached_urls[$_url]); // Cached already. if ($_embed->type === 'error') { continue; // Not possible. } elseif (!empty($_embed->error_code)) { continue; // Not possible. } if ($_markup = $this->embedlyMarkup($_url, $_embed)) { $string = str_replace($_token, $_markup, $string); } } // unset($_token, $_url, $_embed, $_markup); // Housekeeping. # Retrieve embed objects for any uncached URLs now. for ($_i = 1; $_i <= count($uncached_urls); $_i = $_i + 10) { $_urls = array_slice($uncached_urls, $_i - 1, 10); $_urls = array_values($_urls); $_args = ['urls' => $_urls, 'secure' => true, 'width' => 800, 'maxwidth' => 800, 'wmode' => 'transparent']; // See: <http://embed.ly/docs/api/embed/arguments> foreach ($Embedly->oembed($_args) as $_key => $_embed) { $new_embeds[$_urls[$_key]] = $_embed; } // unset($_key, $_embed); // Housekeeping. } // unset($_i, $_urls, $_args); // Housekeeping. # Cache each of the new embeds now, and replace any # tokens associated w/ new embeds obtained above. foreach ($new_embeds as $_url => $_embed) { $this->setEmbedlyCache($_url, $_embed); if ($_embed->type === 'error') { continue; // Not possible. } elseif (!empty($_embed->error_code)) { continue; // Not possible. } if ($_markup = $this->embedlyMarkup($_url, $_embed)) { foreach (array_keys($tokens, $_url, true) as $_token) { $string = str_replace($_token, $_markup, $string); } // unset($_token); // Housekeeping. } } // unset($_url, $_embed, $_markup); // Housekeeping. # Restore URLs that didn't trigger embeds & return. foreach (array_reverse($tokens, true) as $_token => $_url) { $string = str_replace($_token, $_url, $string); } // unset($_token, $_url); // Housekeeping. return $string; }