Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ Add Giphy Support #3763

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sources/ElkArte/AdminController/ManageEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public function action_editorSettings_display()
*/
private function _settings()
{
global $txt;

$config_vars = array(
array('check', 'enableBBC'),
array('check', 'enableBBC', 0, 'onchange' => "toggleBBCDisabled('disabledBBC', !this.checked);"),
Expand All @@ -139,6 +141,10 @@ private function _settings()
array('title', 'editorSettings'),
array('check', 'enableUndoRedo'),
array('check', 'enableSplitTag'),
array('check', 'enableGiphy'),
array('text', 'giphyApiKey', 40, 'subtext' => $txt['giphyApiURL']),
array('select', 'giphyRating', ['g' => 'G', 'pg' => 'PG', 'pg13' => 'PG13', 'r' => 'R']),
array('text', 'giphyLanguage', 5, 'subtext' => $txt['giphyLanguageURL']),

array('title', 'mods_cat_modifications_misc'),
array('check', 'autoLinkUrls'), // @todo not editor or bbc
Expand Down
266 changes: 266 additions & 0 deletions sources/ElkArte/Controller/Giphy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
<?php

/**
* Functions to interact with the Giphy API and return JSON results to the giphy plugin
*
* @package ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
*
* @version 2.0 dev
*
*/

namespace ElkArte\Controller;

use ElkArte\AbstractController;
use ElkArte\Action;
use ElkArte\Errors\Errors;

/**
* Functions to interact with the Giphy API and return JSON results to the giphy plugin
*/
class Giphy extends AbstractController
{
/** @var string $baseApiUrl The base API URL for Giphy. */
protected $baseApiUrl = 'https://api.giphy.com/v1/';

/** @var string The API key used for authentication. */
protected $apiKey;

/** @var array default values to pass to the Giphy API */
protected $config = [
'random_id' => null,
'rating' => 'g',
'lang' => 'en',
'limit' => 28,
];

/**
* pre_dispatch, called before all other methods. Sets the Giphy API key for the Dispatch class.
*
* This method retrieves the Giphy API key from the global $modSettings variable
* @return void
*/
public function pre_dispatch()

Check warning on line 45 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L45

Added line #L45 was not covered by tests
{
global $modSettings;

Check warning on line 47 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L47

Added line #L47 was not covered by tests

// The default is a rate limited 42 search requests an hour and 1000 search requests a day
$this->apiKey = $modSettings['giphyApiKey'] ?? 'fpjXDpZ1cJ0qoqol3BVZz76YHZlv1uB2';

Check warning on line 50 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L50

Added line #L50 was not covered by tests
}

/**
* Index action, based on the SA sends control to the right method.
*
* @return void
*/
public function action_index()

Check warning on line 58 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L58

Added line #L58 was not covered by tests
{
global $context, $modSettings;

Check warning on line 60 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L60

Added line #L60 was not covered by tests

if (empty($modSettings['enableGiphy']))

Check warning on line 62 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L62

Added line #L62 was not covered by tests
{
return;

Check warning on line 64 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L64

Added line #L64 was not covered by tests
}

$this->setConfig();

Check warning on line 67 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L67

Added line #L67 was not covered by tests

$subActions = [
'search' => [$this, 'action_getSearchResults'],
'trending' => [$this, 'action_getTrending'],
];

Check warning on line 72 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L69-L72

Added lines #L69 - L72 were not covered by tests

$action = new Action('giphy');
$subAction = $action->initialize($subActions, 'trending');
$context['sub_action'] = $subAction;
$action->dispatch($subAction);

Check warning on line 77 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L74-L77

Added lines #L74 - L77 were not covered by tests
}

/**
* Sets the configuration settings for the object.
*
* @return array The updated configuration settings after merging with the existing configuration.
*/
public function setConfig()

Check warning on line 85 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L85

Added line #L85 was not covered by tests
{
global $modSettings;

Check warning on line 87 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L87

Added line #L87 was not covered by tests

$config = [
'rating' => $modSettings['giphyRating'] ?? 'g',
'lang' => $modSettings['giphyLanguage'] ?? 'en',
];

Check warning on line 92 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L89-L92

Added lines #L89 - L92 were not covered by tests

$this->config = array_replace($this->config, $config);

Check warning on line 94 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L94

Added line #L94 was not covered by tests

return $this->config;

Check warning on line 96 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L96

Added line #L96 was not covered by tests
}

/**
* Tracks the statistics for a given action.
*
* @return bool Returns false indicating that the statistics tracking is not needed
*/
public function trackStats($action = '')

Check warning on line 104 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L104

Added line #L104 was not covered by tests
{
return false;

Check warning on line 106 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L106

Added line #L106 was not covered by tests
}

/**
* Retrieves trending GIFs.
*
* @return bool The trending GIFs and pagination information.
*/
public function action_getTrending()

Check warning on line 114 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L114

Added line #L114 was not covered by tests
{
checkSession('get');

Check warning on line 116 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L116

Added line #L116 was not covered by tests

is_not_guest();

Check warning on line 118 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L118

Added line #L118 was not covered by tests

$result = $this->request('gifs/trending', [
'random_id' => $this->config['random_id'],
'rating' => $this->config['rating'],
'limit' => $this->config['limit'],
'offset' => $this->_req->getQuery('offset', 'intval', 0)
], $error);

Check warning on line 125 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L120-L125

Added lines #L120 - L125 were not covered by tests

if ($error)

Check warning on line 127 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L127

Added line #L127 was not covered by tests
{
return $this->sendResults([], []);

Check warning on line 129 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L129

Added line #L129 was not covered by tests
}

$images = $this->prepareImageResults($result);
$result['pagination']['limit'] = $this->config['limit'];

Check warning on line 133 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L132-L133

Added lines #L132 - L133 were not covered by tests

return $this->sendResults($images, $result);

Check warning on line 135 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L135

Added line #L135 was not covered by tests
}

/**
* Retrieves search results for GIFs based on the provided query.
*
* @return bool The search results and pagination information.
*/
public function action_getSearchResults()

Check warning on line 143 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L143

Added line #L143 was not covered by tests
{
checkSession('get');

Check warning on line 145 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L145

Added line #L145 was not covered by tests

is_not_guest();

Check warning on line 147 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L147

Added line #L147 was not covered by tests

$result = $this->request('gifs/search', [
'q' => $this->_req->getQuery('q', 'trim', ''),
'random_id' => $this->config['random_id'],
'rating' => $this->config['rating'],
'limit' => $this->config['limit'],
'offset' => $this->_req->getQuery('offset', 'intval', 0)
], $error);

Check warning on line 155 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L149-L155

Added lines #L149 - L155 were not covered by tests

if ($error)

Check warning on line 157 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L157

Added line #L157 was not covered by tests
{
return $this->sendResults([], []);

Check warning on line 159 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L159

Added line #L159 was not covered by tests
}

$images = $this->prepareImageResults($result);
$result['pagination']['limit'] = $this->config['limit'];

Check warning on line 163 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L162-L163

Added lines #L162 - L163 were not covered by tests

return $this->sendResults($images,$result);

Check warning on line 165 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L165

Added line #L165 was not covered by tests
}

/**
* Sets the results in context so the JSON template can deliver them.
*
* @param array $images An array of trending GIFs.
* @param array $result The pagination and meta information.
*
* @return bool Returns true after sending the results.
*/
public function sendResults($images, $result)

Check warning on line 176 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L176

Added line #L176 was not covered by tests
{
global $context;

Check warning on line 178 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L178

Added line #L178 was not covered by tests

theme()->getLayers()->removeAll();
theme()->getTemplates()->load('Json');
$context['sub_template'] = 'send_json';

Check warning on line 182 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L180-L182

Added lines #L180 - L182 were not covered by tests

$context['json_data'] = [
'giphy' => $images,
'data' => $result
];

Check warning on line 187 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L184-L187

Added lines #L184 - L187 were not covered by tests

return true;

Check warning on line 189 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L189

Added line #L189 was not covered by tests
}

/**
* Sends a request to the GIPHY API.
*
* @param string $path The API endpoint path.
* @param array $params The additional parameters for the request (optional).
* @param string &$error A variable to hold any error message (optional).
*
* @return array The response from the API as an associative array, or an empty array if there was an error.
*/
public function request(string $path, array $params = [], string &$error = null): array

Check warning on line 201 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L201

Added line #L201 was not covered by tests
{
$result = [];
$params = ['api_key' => $this->apiKey] + $params;
$path .= '?' . http_build_query($params, '','&');

Check warning on line 205 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L203-L205

Added lines #L203 - L205 were not covered by tests

require_once(SUBSDIR . '/Package.subs.php');
$body = fetch_web_data($this->baseApiUrl . $path);
if ($body !== false)

Check warning on line 209 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L207-L209

Added lines #L207 - L209 were not covered by tests
{
$contents = json_decode($body, true);

Check warning on line 211 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L211

Added line #L211 was not covered by tests

return is_array($contents) ? $contents : [];

Check warning on line 213 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L213

Added line #L213 was not covered by tests
}

$error = true;
Errors::instance()->log_error('GIPHY API error');

Check warning on line 217 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L216-L217

Added lines #L216 - L217 were not covered by tests

return $result;

Check warning on line 219 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L219

Added line #L219 was not covered by tests
}

/**
* Prepares the results from the API response.
*
* @param array $result The API response containing the image data.
* @return array The prepared image results.
*/
protected function prepareImageResults($result): array

Check warning on line 228 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L228

Added line #L228 was not covered by tests
{
$images = [];

Check warning on line 230 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L230

Added line #L230 was not covered by tests

if (is_array($result))

Check warning on line 232 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L232

Added line #L232 was not covered by tests
{
foreach ($result['data'] as $data)

Check warning on line 234 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L234

Added line #L234 was not covered by tests
{
$fixedHeight = $data['images']['fixed_height']['url'];
$fixedHeightStill = $data['images']['fixed_height_still']['url'];

Check warning on line 237 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L236-L237

Added lines #L236 - L237 were not covered by tests

$fixedHeightSmall = $data['images']['fixed_height_small']['url'] ?? $fixedHeight;
$fixedHeightSmallStill = $data['images']['fixed_height_small_still']['url'] ?? $fixedHeightStill;

Check warning on line 240 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L239-L240

Added lines #L239 - L240 were not covered by tests

$images[$data['id']] = [
'title' => $data['title'],
'insert' => $this->normalizeUrl($fixedHeight),
'src' => $this->normalizeUrl($fixedHeightSmall),
'thumbnail' => $this->normalizeUrl($fixedHeightSmallStill),
];

Check warning on line 247 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L242-L247

Added lines #L242 - L247 were not covered by tests
}
}

return $images;

Check warning on line 251 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L251

Added line #L251 was not covered by tests
}

/**
* Normalizes a given URL.
*
* @param string $url The URL to be normalized.
* @return string The normalized URL without query parameters or fragments.
*/
protected function normalizeUrl($url)

Check warning on line 260 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L260

Added line #L260 was not covered by tests
{
$parts = parse_url($url);

Check warning on line 262 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L262

Added line #L262 was not covered by tests

return sprintf('%s://%s%s', $parts['scheme'], $parts['host'], $parts['path']);

Check warning on line 264 in sources/ElkArte/Controller/Giphy.php

View check run for this annotation

Codecov / codecov/patch

sources/ElkArte/Controller/Giphy.php#L264

Added line #L264 was not covered by tests
}
}
8 changes: 7 additions & 1 deletion sources/ElkArte/Languages/Admin/English.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,15 @@
$txt['bbcTagsToUse'] = 'Enabled BBC tags';
$txt['bbcTagsToUse_select'] = 'Select the tags allowed to be used';
$txt['bbcTagsToUse_select_all'] = 'Select all tags';
$txt['editorSettings'] = 'Editor Options';
$txt['editorSettings'] = 'Editor Plugins';
$txt['enableUndoRedo'] = 'Enable the undo/redo plugin';
$txt['enableSplitTag'] = 'Enable the split tag plugin';
$txt['enableGiphy'] = 'Enable the Giphy plugin';
$txt['giphyApiKey'] = 'Enter you Giphy API key';
$txt['giphyRating'] = 'Permitted Giphy rating';
$txt['giphyLanguage'] = 'Giphy language code, defaults to en';
$txt['giphyLanguageURL'] = '<a href="https://developers.giphy.com/docs/optional-settings/#language-support">Language Codes</a>';
$txt['giphyApiURL'] = '<a href="https://support.giphy.com/hc/en-us/articles/360020283431-Request-A-GIPHY-API-Key">Request API Key</a>';

$txt['enableParticipation'] = 'Enable participation icons';
$txt['enableFollowup'] = 'Enable followups';
Expand Down
2 changes: 1 addition & 1 deletion sources/ElkArte/Languages/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function getForEditing()
}

/**
* Save method saves content to a file with the specified file name.
* Save method saves content to the DB with the specified file name.
*
* @param string $file_name The name of the file to save the content to.
* @param array $txt The array of content to save.
Expand Down
3 changes: 2 additions & 1 deletion sources/ElkArte/Languages/Editor/English.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@
$editortxt['Insert Spoiler'] = 'Insert Spoiler';
$editortxt['Insert Footnote'] = 'Insert Footnote';
$editortxt['Split Tag'] = 'Split quote at cursor (ctrl+enter)';
$editortxt['Description (optional):'] = 'Description (optional):';
$editortxt['Description (optional):'] = 'Description (optional):';
$editortxt['Insert Giphy'] = 'Insert a Giphy GIF';
Loading
Loading