shortname = $shortname;
$this->pluginFilePath = $pluginFilePath;
$this->version = $version;
$this->pluginName = $pluginName;
$this->platformName = $platformName;
}
public function getShortName()
{
return $this->shortname;
}
public function getWebhookAction()
{
return 'trustindex_feed_hook_' . $this->getShortName();
}
public function getWebhookUrl()
{
$webhookUrl = rest_url($this->getWebhookAction());
$response = wp_remote_get('https://admin.trustindex.io/api/testWordpressWebbookUrl?webhook=' . $webhookUrl, [
'timeout' => 30,
'sslverify' => false,
]);
if (is_wp_error($response)) {
return null;
}
$json = json_decode(wp_remote_retrieve_body($response), true);
if (!$json || !isset($json['valid']) || true !== $json['valid']) {
return null;
}
return $webhookUrl;
}
public function getPluginTabs()
{
$tabs = [];
$tabs[] = [
'place' => 'left',
'slug' => 'feed-configurator',
'name' => __('Feed Configurator', 'social-photo-feed-widget')
];
if ($this->getConnectedSource()) {
$tabs[] = [
'place' => 'left',
'slug' => 'my-posts',
'name' => __('My Posts', 'social-photo-feed-widget')
];
}
$tabs[] = [
'place' => 'left',
'slug' => 'get-more-features',
'name' => __('Get more features', 'social-photo-feed-widget')
];
$tabs[] = [
'place' => 'right',
'slug' => 'advanced',
'name' => __('Advanced', 'social-photo-feed-widget')
];
return $tabs;
}
public function getPluginDir()
{
return plugin_dir_path($this->pluginFilePath);
}
public function getPluginFileUrl($file, $addVersioning = true)
{
$info = pathinfo($file);
if (!isset($info['dirname'], $info['basename'], $info['extension'])) {
return $file;
}
$url = plugins_url($file, $this->pluginFilePath);
if ($addVersioning) {
$appendMark = strpos($url, '?') === FALSE ? '?' : '&';
$url .= $appendMark . 'ver=' . $this->getVersion();
}
return $url;
}
public function displayImg($image_url, $attributes = array())
{
$isUrl = preg_match('#^https?://#i', $image_url);
if (!$isUrl) {
$image_url = $this->getPluginFileUrl($image_url);
}
$defaults = array(
'src' => $isUrl ? esc_url($image_url) : sanitize_text_field($image_url),
'alt' => '',
'class' => '',
'style' => '',
);
$attributes = wp_parse_args($attributes, $defaults);
$attr_string = '';
foreach ($attributes as $key => $value) {
$attr_string .= sprintf(' %s="%s"', esc_attr($key), esc_attr($value));
}
return sprintf('
', $attr_string);
}
public function getPluginSlug()
{
return basename($this->getPluginDir());
}
public function getPluginCurrentVersion()
{
$response = wp_remote_get('https://api.wordpress.org/plugins/info/1.2/?action=plugin_information&request[slug]='. $this->getPluginSlug());
$json = json_decode($response['body'], true);
if (!$json || !isset($json['version'])) {
return false;
}
return $json['version'];
}
public function activate()
{
$requestBody = [
'platform' => 'Instagram',
'website' => get_option('siteurl'),
];
$response = wp_remote_post('https://admin.trustindex.io/new/wordpress-feed/register', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'ti-secure' => hash_hmac('sha256', http_build_query($requestBody), '80ce0e06b31b34794f5088d4875480f1'),
],
'body' => $requestBody,
'timeout' => '30',
'sslverify' => false,
]);
if (is_wp_error($response)) {
update_option($this->getOptionName('public-id'), $response->get_error_message(), false);
return;
}
$data = json_decode(wp_remote_retrieve_body($response), true);
update_option($this->getOptionName('public-id'), $data['public-id'] ?? $data['error'], false);
include $this->getPluginDir() . 'include' . DIRECTORY_SEPARATOR . 'activate.php';
if (!$this->getNotificationParam('rate-us', 'hidden', false) && $this->getNotificationParam('rate-us', 'active', true)) {
$this->setNotificationParam('rate-us', 'active', true);
$this->setNotificationParam('rate-us', 'timestamp', time() + 86400);
}
update_option($this->getOptionName('activation-redirect'), 1, false);
}
public function load()
{
$this->loadI18N();
include $this->getPluginDir() . 'include' . DIRECTORY_SEPARATOR . 'update.php';
if (get_option($this->getOptionName('activation-redirect'))) {
delete_option($this->getOptionName('activation-redirect'));
wp_safe_redirect(admin_url('admin.php?page=' . $this->getPluginSlug() . '/admin.php'));
exit;
}
$tokenExpireTimestamp = (int)get_option($this->getOptionName('token-expires'));
$isNotificationEnabled = $this->isNotificationEnabled('token-renew');
if ($tokenExpireTimestamp &&
$tokenExpireTimestamp < time() + (86400 * 7) &&
($this->getNotificationParam('token-renew', 'do-check', true) || !$isNotificationEnabled)
) {
$this->setNotificationParam('token-renew', 'active', $isNotificationEnabled);
$this->setNotificationParam('token-renew', 'do-check', false);
$this->setNotificationParam('token-expired', 'do-check', true);
}
$isNotificationEnabled = $this->isNotificationEnabled('token-expired');
if ($tokenExpireTimestamp &&
$tokenExpireTimestamp < time() &&
($this->getNotificationParam('token-expired', 'do-check', true) || !$isNotificationEnabled)
) {
$this->setNotificationParam('token-renew', 'active', false);
$this->setNotificationParam('token-expired', 'active', $isNotificationEnabled);
$this->setNotificationParam('token-expired', 'do-check', false);
}
$isNotificationEnabled = $this->isNotificationEnabled('post-download-available');
if ($isNotificationEnabled &&
$this->getConnectedSource() &&
!$this->isDownloadInProgress() &&
$this->getDownloadAvailableTimestamp() < time() &&
!$this->getNotificationParam('post-download-available', 'hidden') &&
$this->getNotificationParam('post-download-available', 'do-check', true)
) {
$this->setNotificationParam('post-download-available', 'active', true);
$this->setNotificationParam('post-download-available', 'do-check', false);
}
}
public function deactivate()
{
update_option($this->getOptionName('active'), '0');
}
public function uninstall()
{
$this->deleteConnectedSource();
include $this->getPluginDir() . 'include' . DIRECTORY_SEPARATOR . 'uninstall.php';
if (is_file($this->getCssFile())) {
wp_delete_file($this->getCssFile());
}
}
public function outputBuffer()
{
ob_start();
}
public function loadI18N()
{
load_textdomain(
$this->getPluginSlug(),
$this->getPluginDir().'/languages/'.$this->getPluginSlug().'-'.get_locale().'.mo'
);
}
public function getShortcodeName($isAdmin = false)
{
return 'trustindex-feed'.($isAdmin ? '' : '-'.$this->getShortName());
}
public function shortcode()
{
$pluginManager = $this;
add_shortcode($this->getShortcodeName(), function($atts) use($pluginManager) {
if (!$pluginManager->getConnectedSource()) {
return $pluginManager->errorBoxForAdmins(__('You have to connect your source!', 'social-photo-feed-widget'));
}
return '
' .
''. __('ERROR with the following plugin:', 'social-photo-feed-widget') .' '. $this->pluginName .'
' .
__('CSS file could not saved.', 'social-photo-feed-widget') .' ('. $this->getCssFile() .') '. __('Your widgets do not display properly!', 'social-photo-feed-widget') . '
';
if ($errorType === 'filesystem') {
$html .= '
There is an error with your filesystem. We got the following error message:
'. $errorMessage .'
Maybe you configured your filesystem incorrectly.
Here you can read about how to configure filesystem in your WordPress.';
}
else {
if ($fileExists) {
$html .= __('CSS file exists and it is not writeable. Delete the file', 'social-photo-feed-widget');
}
else {
$html .= __('Grant write permissions to upload folder', 'social-photo-feed-widget');
}
$html .= '
' .
__('or', 'social-photo-feed-widget') . '
' .
/* translators: %s: URL of Advanced page */
sprintf(__("enable 'CSS internal loading' in the %s page!", 'social-photo-feed-widget'), '
prefix .'trustindex_feed_' . $name;
}
public function isTableExists($name = "")
{
return false;
}
public function addSettingMenu()
{
global $menu, $submenu;
$permission = 'edit_pages';
$adminPageUrl = $this->getPluginSlug() . "/admin.php";
$adminPageTitle = $this->platformName . ' Feed';
$menuBadge = '';
if ($this->getNotificationParam('token-expired', 'active', false)) {
$menuBadge = '1';
}
add_menu_page(
$adminPageTitle,
$adminPageTitle.$menuBadge,
$permission,
$adminPageUrl,
'',
$this->getPluginFileUrl('assets/img/trustindex-sign-logo.png')
);
}
public function addPluginActionLinks($links, $file)
{
if (basename($file) === $this->getPluginSlug() . '.php') {
$platformLink = '';
if (!get_option($this->getOptionName('source'), 0)) {
/* translators: %s: Platform name */
$platformLink .= sprintf(__('Connect %s', 'social-photo-feed-widget'), $this->platformName);
} elseif (!get_option($this->getOptionName('css-content'), 0)) {
$platformLink .= __('Create Widget', 'social-photo-feed-widget');
} else {
$platformLink .= __('Widget Settings', 'social-photo-feed-widget');
}
$platformLink .= '';
array_unshift($links, $platformLink);
}
return $links;
}
public function addPluginMetaLinks($meta, $file)
{
if (basename($file) === $this->getPluginSlug() . '.php') {
$meta[] = '
'.__('Get more Features', 'social-photo-feed-widget').' →';
$meta[] = '
'.__('Rate our plugin', 'social-photo-feed-widget').' ★★★★★';
}
return $meta;
}
public function addScripts($hook)
{
$tmp = explode('/', $hook);
$currentSlug = array_shift($tmp);
if ($this->getPluginSlug() === $currentSlug) {
if (file_exists($this->getPluginDir() . 'assets' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'admin.css')) {
wp_enqueue_style('trustindex-feed-admin-'. $this->getShortName(), $this->getPluginFileUrl('assets/css/admin.css'), [], $this->getVersion());
}
if (file_exists($this->getPluginDir() . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'admin.js')) {
wp_enqueue_script('trustindex-feed-admin-'. $this->getShortName(), $this->getPluginFileUrl('assets/js/admin.js'), [], $this->getVersion(), [ 'in_footer' => false ]);
wp_localize_script('trustindex-feed-admin-'. $this->getShortName(), 'ajax_object', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ti-download-check'),
'interval' => self::$downloadCheckSeconds * 1000,
]);
}
}
wp_register_script('trustindex_admin_notification', $this->getPluginFileUrl('assets/js/admin-notification.js'), [], $this->getVersion(), [ 'in_footer' => false ]);
wp_enqueue_script('trustindex_admin_notification');
wp_enqueue_style('trustindex_admin_notification', $this->getPluginFileUrl('assets/css/admin-notification.css'), [], $this->getVersion());
}
public static function getAlertBox($type, $content, $newline_content = true)
{
$types = [
'warning' => [
'css' => 'color: #856404; background-color: #fff3cd; border-color: #ffeeba;',
'icon' => 'dashicons-warning'
],
'info' => [
'css' => 'color: #0c5460; background-color: #d1ecf1; border-color: #bee5eb;',
'icon' => 'dashicons-info'
],
'error' => [
'css' => 'color: #721c24; background-color: #f8d7da; border-color: #f5c6cb;',
'icon' => 'dashicons-info'
]
];
return '
'
. ' '. strtoupper($type) .''
. ($newline_content ? '
' : "")
. $content
. '
';
}
public function errorBoxForAdmins($text)
{
if (!current_user_can(self::$permissionNeeded)) {
return "";
}
return self::getAlertBox('error', ' @
Trustindex plugin ('. __('This message is not be visible to visitors in public mode.', 'social-photo-feed-widget') .')'. $text, false);
}
public function getNotificationOptions($type = "")
{
$tokenExpireDate = gmdate('Y-m-d H:i', (int)get_option($this->getOptionName('token-expires')));
$list = [
'rate-us' => [
'type' => 'warning',
'extra-class' => 'trustindex-popup',
'button-text' => "",
'is-closeable' => true,
'hide-on-close' => false,
'hide-on-open' => true,
'remind-later-button' => false,
'redirect' => 'https://wordpress.org/support/plugin/'. $this->getPluginSlug() .'/reviews/?rate=5#new-post',
'text' =>
/* translators: %s: Name of the plugin */
sprintf(__('We have worked a lot on the free "%s" plugin.', 'social-photo-feed-widget'), $this->pluginName) . '
' .
__('If you love our features, please write a review to help us make the plugin even better.', 'social-photo-feed-widget') . '
' .
/* translators: %s: Trustindex CEO */
sprintf(__('Thank you. Gabor, %s', 'social-photo-feed-widget'), 'Trustindex CEO'),
],
'token-renew' => [
'type' => 'warning',
'extra-class' => "",
'button-text' => __('Go to Connect Page', 'social-photo-feed-widget'),
'is-closeable' => false,
'hide-on-close' => false,
'hide-on-open' => false,
'remind-later-button' => false,
'redirect' => '?page='. $this->getPluginSlug() .'/admin.php&tab=feed-configurator&step=1&reconnect-source',
'text' =>
'
'.
__('Important: ', 'social-photo-feed-widget').
/* translators: 1: Platform name, 2: Date string */
sprintf(__('Your %1$s Access Token expires on %2$s.', 'social-photo-feed-widget'), ucfirst($this->getShortName()), $tokenExpireDate).
''.
__('Please renew your token by clicking the "Reconnect" button on the Connect Page.', 'social-photo-feed-widget').'
'.
/* translators: %s: Platform name */
sprintf(__('This will ensure that your %s Feed Widget continues to update automatically.', 'social-photo-feed-widget'), ucfirst($this->getShortName())),
],
'token-expired' => [
'type' => 'error',
'extra-class' => "",
'button-text' => __('Go to Connect Page', 'social-photo-feed-widget'),
'is-closeable' => false,
'hide-on-close' => false,
'hide-on-open' => false,
'remind-later-button' => false,
'redirect' => '?page='. $this->getPluginSlug() .'/admin.php&tab=feed-configurator&step=1&reconnect-source',
'text' =>
'
'.
__('Important: ', 'social-photo-feed-widget').
/* translators: 1: Platform name, 2: Date string */
sprintf(__('Your %1$s Access Token expired on %2$s.', 'social-photo-feed-widget'), ucfirst($this->getShortName()), $tokenExpireDate).
''.
__('Please renew your token by clicking the "Reconnect" button on the Connect Page.', 'social-photo-feed-widget').'
'.
/* translators: %s: Platform name */
sprintf(__('This will ensure that your %s Feed Widget continues to update automatically.', 'social-photo-feed-widget'), ucfirst($this->getShortName())),
'short-message' =>
$this->displayImg(str_replace('%platform%', ucfirst($this->getShortName()), 'https://cdn.trustindex.io/assets/platform/%platform%/icon-feed.svg'), array('alt' => ucfirst($this->getShortName()))).
'
'.
'' . __('Important: ', 'social-photo-feed-widget') . ''.
/* translators: %s: Platform name */
sprintf(__('We can no longer update the posts in your %s feed widget.', 'social-photo-feed-widget'), ucfirst($this->getShortName())).
'
'. __('Click here to reconnect', 'social-photo-feed-widget') .''.
'
',
],
'post-download-available' => [
'type' => 'warning',
'extra-class' => "",
'button-text' => __('Download your latest posts! »', 'social-photo-feed-widget'),
'is-closeable' => true,
'hide-on-close' => true,
'hide-on-open' => true,
'remind-later-button' => false,
'redirect' => '?page='.$this->getPluginSlug().'/admin.php&tab=my-posts',
/* translators: %s: Platform name */
'text' => sprintf(__('You can update your %s feed posts.', 'social-photo-feed-widget'), ucfirst($this->getShortName())),
],
'post-download-finished' => [
'type' => 'warning',
'extra-class' => "",
'button-text' => __('Check your latest posts! »', 'social-photo-feed-widget'),
'is-closeable' => true,
'hide-on-close' => true,
'hide-on-open' => true,
'remind-later-button' => false,
'redirect' => '?page='.$this->getPluginSlug().'/admin.php&tab=my-posts',
/* translators: %s: Platform name */
'text' => sprintf(__('Your new %s posts have been downloaded.', 'social-photo-feed-widget'), ucfirst($this->getShortName())),
],
'connect-finished' => [
'type' => 'info',
'extra-class' => "",
/* translators: %s: Platform name */
'button-text' => sprintf(__('Create %s Feed Widget', 'social-photo-feed-widget'), ucfirst($this->getShortName())),
'is-closeable' => true,
'hide-on-close' => true,
'hide-on-open' => true,
'remind-later-button' => false,
'redirect' => '?page='.$this->getPluginSlug().'/admin.php&tab=feed-configurator&step=2',
'text' =>
'
'.
/* translators: %s: Platform name */
sprintf(__('%s posts ready', 'social-photo-feed-widget'), ucfirst($this->getShortName())).
''.
/* translators: %s: Platform name */
sprintf(__('Your %s posts are imported and ready.', 'social-photo-feed-widget'), ucfirst($this->getShortName())).'
'.
__('Create and embed your feed widget', 'social-photo-feed-widget'),
'short-message' =>
$this->displayImg(str_replace('%platform%', ucfirst($this->getShortName()), 'https://cdn.trustindex.io/assets/platform/%platform%/icon-feed.svg'), array('alt' => ucfirst($this->getShortName()))).
'
'.
''.
/* translators: %s: Platform name */
sprintf(__('%s posts ready', 'social-photo-feed-widget'), ucfirst($this->getShortName())).
'
'.
/* translators: %s: Platform name */
sprintf(__('Your %s posts are imported and ready.', 'social-photo-feed-widget'), ucfirst($this->getShortName())).'
'.
__('Create and embed your feed widget', 'social-photo-feed-widget').'
'.
/* translators: %s: Platform name */
''.sprintf(__('Create %s Feed Widget', 'social-photo-feed-widget'), ucfirst($this->getShortName())).''.
'
',
],
];
return $type ? $list[$type] : $list;
}
public function getNotificationActionUrl($type, $action, $remindDays = null)
{
return wp_nonce_url(
add_query_arg(
array_filter(array(
'page' => $this->getPluginSlug() . '/admin.php',
'notification' => $type,
'action' => $action,
'remind-days' => $remindDays,
)),
admin_url('admin.php')
),
'ti-notification'
);
}
public function setNotificationParam($type, $param, $value)
{
$notifications = get_option($this->getOptionName('notifications'), []);
if (!isset($notifications[ $type ])) {
$notifications[ $type ] = [];
}
$notifications[ $type ][ $param ] = $value;
update_option($this->getOptionName('notifications'), $notifications, false);
}
public function getNotificationParam($type, $param, $default = null)
{
$notifications = get_option($this->getOptionName('notifications'), []);
if (!isset($notifications[ $type ]) || !isset($notifications[ $type ][ $param ])) {
return $default;
}
return $notifications[ $type ][ $param ];
}
public function isNotificationActive($type)
{
$notifications = get_option($this->getOptionName('notifications'), []);
if (
!isset($notifications[ $type ]) ||
!isset($notifications[ $type ]['active']) || !$notifications[ $type ]['active'] ||
(isset($notifications[ $type ]['hidden']) && $notifications[ $type ]['hidden']) ||
(isset($notifications[ $type ]['timestamp']) && $notifications[ $type ]['timestamp'] > time())
) {
return false;
}
return true;
}
public function isNotificationEnabled($type)
{
$notifications = get_option($this->getOptionName('notifications'), []);
return isset($notifications[$type]);
}
public function getNotificationEmailContent($type)
{
$subject = '';
$message = '';
$username = '';
$source = get_option($this->getOptionName('connect-pending'), []);
if (isset($source['username'])) {
$username = $source['username'];
} elseif ($source = $this->getConnectedSource()) {
$username = $source['name'];
}
switch ($type) {
case 'connect-finished':
$link = admin_url('admin.php?page='.$this->getPluginSlug().'/admin.php&tab=feed-configurator&step=2');
$subject = 'Create your Instagram feed widget';
$message = strtr(
'
Create your Instagram feed widget
| |
|
Your Instagram account (@%username%) has been successfully connected and the posts have been downloaded – you can now create your Instagram feed widget. Create Instagram Feed Widget |
2018-2026 © Trustindex.io
https://www.trustindex.io
|
',
[
'%username%' => $username,
'%link%' => $link,
]
);
break;
case 'post-download-finished':
$link = admin_url('admin.php?page='.$this->getPluginSlug().'/admin.php&tab=my-posts');
$subject = 'New posts have been added to your feed';
$message = strtr(
'
New posts have been added to your feed
| |
Your Instagram account (@%username%) has been refreshed and new posts are now available. Check out the latest posts on your list and manage your feed.View Posts |
2018-2026 © Trustindex.io
https://www.trustindex.io
|
',
[
'%username%' => $username,
'%link%' => $link,
]
);
break;
}
return [
'subject' => $subject,
'message' => $message,
];
}
public function sendNotificationEmail($type)
{
if ($email = $this->getNotificationParam($type, 'email', get_option('admin_email'))) {
if (!$this->getNotificationParam($type, 'hidden')) {
$this->setNotificationParam($type, 'do-check', true);
$this->setNotificationParam($type, 'active', false);
}
$msg = $this->getNotificationEmailContent($type);
if ($msg['subject'] && $msg['message']) {
try {
return wp_mail($email, $msg['subject'], $msg['message'], ['Content-Type: text/html; charset=UTF-8'], ['']);
} catch (Exception $e) {
return false;
}
}
}
}
public function getOptionName($optName)
{
if (!in_array($optName, $this->getOptionNames()) && !in_array($optName, $this->getNotUsedOptionNames())) {
echo "Option not registered in plugin (TrustindexFeed class)";
}
if (in_array($optName, [ 'proxy-check', 'cdn-version-control' ])) {
return 'trustindex-'. $optName;
}
else {
return 'trustindex-feed-'. $this->getShortName() .'-'. $optName;
}
}
public function getOptionNames()
{
return [
'active',
'activation-redirect',
'proxy-check',
'notifications',
'rate-us-feedback',
'cdn-version-control',
'version-control',
'preview',
'source',
'connect-pending',
'feed-data',
'feed-data-saved',
'feed-data-downloaded',
'feed-data-download-checked',
'public-id',
'token-expires',
'css-content',
'load-css-inline',
'layout',
'template',
];
}
public function getNotUsedOptionNames()
{
return [
'version',
'update-version-check',
];
}
public function getCdnVersionControl()
{
return get_option($this->getOptionName('cdn-version-control'), []);
}
public function getCdnVersion($name = "")
{
$data = $this->getCdnVersionControl();
return isset($data[ $name ]) ? $data[ $name ] : "";
}
public function getVersion($name = "")
{
if (!$name) {
return $this->version;
}
$data = get_option($this->getOptionName('version-control'), []);
return isset($data[ $name ]) ? $data[ $name ] : "1.0";
}
public function updateVersion($name, $value)
{
$data = get_option($this->getOptionName('version-control'), []);
$data[ $name ] = $value;
return update_option($this->getOptionName('version-control'), $data, false);
}
public function getAuthError(WP_REST_Request $request)
{
$signature = $request->get_header('X-Signature');
$timestamp = (int) $request->get_header('X-Timestamp');
if (1800 < (time() - $timestamp)) {
return new WP_Error('expired', 'Request expired', ['status' => 401]);
}
$publicId = get_option($this->getOptionName('public-id'));
if (!$publicId) {
return new WP_Error('missing_public_id', 'Public ID is missing', ['status' => 400]);
}
$body = $request->get_body();
$expected = hash_hmac('sha256', $body.$timestamp, $publicId);
if (!hash_equals($expected, $signature)) {
return new WP_Error('invalid_signature', 'Signature mismatch', ['status' => 403]);
}
return null;
}
}
?>