<?php

/*
 * This file is part of the Fancy Bricks package.
 *
 * (c) Joshua Gugun Siagian <suabahasa@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare (strict_types=1);
namespace Fancy\Animations\Utils;

use Exception;
use _FancyAnimations\FANCY_ANIMATIONS;
/**
 * Common utility functions for the plugin.
 */
class Common
{
    /**
     * Save the worker result into the file.
     *
     * @param string $content The content of the file.
     * @param string $file_path The file path.
     * @param int $flags The flags to pass to the file_put_contents() function.
     * @throws Exception
     */
    public static function save_file($content, $file_path, $flags = 0) : void
    {
        if (!\file_exists($file_path)) {
            \wp_mkdir_p(\dirname($file_path));
        }
        $result = \file_put_contents($file_path, $content, $flags);
        if ($result === \false) {
            throw new Exception('Failed to save to file.', 500);
        }
        // if write is successful continue
        $saved_content = \file_get_contents($file_path);
        // if read is successful continue
        if ($saved_content === \false) {
            throw new Exception('The saved file is not readable.', 500);
        }
    }
    /**
     * Get the plugin's data.
     *
     * @param string $key The key to retrieve.
     * @return mixed
     */
    public static function plugin_data(?string $key = null)
    {
        if (!\function_exists('get_plugin_data')) {
            require_once \ABSPATH . 'wp-admin/includes/plugin.php';
        }
        $plugin_data = \wp_cache_get('plugin_data', FANCY_ANIMATIONS::WP_OPTION);
        if (!$plugin_data) {
            $plugin_data = \get_plugin_data(FANCY_ANIMATIONS::FILE);
            \wp_cache_set('plugin_data', $plugin_data, FANCY_ANIMATIONS::WP_OPTION);
        }
        return $key ? $plugin_data[$key] : $plugin_data;
    }
    /**
     * Redirect to the given location. If headers are already sent, use a meta refresh.
     *
     * @param string $location The location to redirect to.
     * @param bool $safe Whether to use wp_safe_redirect() or not.
     */
    public static function redirect(string $location, bool $safe = \false)
    {
        if (!\headers_sent()) {
            if ($safe) {
                \wp_safe_redirect($location);
            } else {
                \wp_redirect($location);
            }
        } else {
            echo '<meta http-equiv="refresh" content="0;url=' . \esc_url($location) . '">';
        }
        exit;
    }
}
