| | 903 | |
| | 904 | /** |
| | 905 | * Load hooks to prevent plugin updates if PHP version requirement not met. |
| | 906 | */ |
| | 907 | public function load_requires_php() { |
| | 908 | add_filter( 'plugin_row_meta', array( $this, 'plugin_update_nag' ), 10, 2 ); |
| | 909 | } |
| | 910 | |
| | 911 | /** |
| | 912 | * Adds small PHP upgrade nag to plugin row. |
| | 913 | * |
| | 914 | * @param array $links |
| | 915 | * @param string $file |
| | 916 | * |
| | 917 | * @return array $links |
| | 918 | */ |
| | 919 | public function plugin_update_nag( $links, $file ) { |
| | 920 | $slug = dirname( $file ); |
| | 921 | if ( $this->is_required_php( $slug ) ) { |
| | 922 | $links[] = '<span style="color:#f00;">' . __( 'Upgrade PHP for available plugin update.' ) . '</span>'; |
| | 923 | add_action( "after_plugin_row_{$file}", array( $this, 'hide_plugin_update_row' ), 10, 2 ); |
| | 924 | } |
| | 925 | |
| | 926 | return $links; |
| | 927 | } |
| | 928 | |
| | 929 | /** |
| | 930 | * Write out inline style to hide the update row notice. |
| | 931 | * |
| | 932 | * @param string $plugin_file Unused. |
| | 933 | * @param array $plugin_data Plugin data. |
| | 934 | */ |
| | 935 | public function hide_plugin_update_row( $plugin_file, $plugin_data ) { |
| | 936 | print( '<style type="text/css">tr#' . $plugin_data['slug'] . '-update{ display:none; }</style>' ); |
| | 937 | print( '<script>jQuery(".update[data-plugin=\'' . $plugin_file . '\']").removeClass("update");</script>' ); |
| | 938 | } |
| | 939 | |
| | 940 | /** |
| | 941 | * Unset update_plugins from update-core.php as appropriate. |
| | 942 | * |
| | 943 | * @param array $plugins Plugins for updating. |
| | 944 | * |
| | 945 | * @return array $plugins Plugins for updating. |
| | 946 | */ |
| | 947 | public function unset_plugin_updates( $plugins ) { |
| | 948 | if ( ! empty( $plugins ) ) { |
| | 949 | foreach ( $plugins as $file => $class ) { |
| | 950 | if ( $this->is_required_php( $class->update->slug ) ) { |
| | 951 | unset( $plugins[ $file ] ); |
| | 952 | $this->requires_php_update_notice(); |
| | 953 | } |
| | 954 | } |
| | 955 | } |
| | 956 | |
| | 957 | return $plugins; |
| | 958 | } |
| | 959 | |
| | 960 | /** |
| | 961 | * Create requires PHP update notice. |
| | 962 | */ |
| | 963 | public function requires_php_update_notice() { |
| | 964 | $message = '<span style="color:#f00;" class="dashicons dashicons-warning"></span> '; |
| | 965 | $message .= __( 'Some updates are not shown in this list because they require a newer version of PHP.' ); |
| | 966 | print( '<div class="notice-error notice"><p>' . $message . '</p></div>' ); |
| | 967 | } |
| | 968 | |
| | 969 | /** |
| | 970 | * Test for required PHP version. |
| | 971 | * |
| | 972 | * @param string $slug Slug of the repository being tested. |
| | 973 | * |
| | 974 | * @return bool True for below required PHP version. |
| | 975 | * False for above required PHP version or none set. |
| | 976 | */ |
| | 977 | protected function is_required_php( $slug ) { |
| | 978 | $response = $this->get_plugin_dot_org_api_data( $slug ); |
| | 979 | |
| | 980 | if ( ! $response->requires_php ) { |
| | 981 | return false; |
| | 982 | } |
| | 983 | |
| | 984 | return version_compare( PHP_VERSION, $response->requires_php, '<=' ); |
| | 985 | } |
| | 986 | |
| | 987 | /** |
| | 988 | * Get the dot org API data for the plugin or theme slug. |
| | 989 | * |
| | 990 | * @param string $slug Plugin or theme slug. |
| | 991 | * |
| | 992 | * @return object|bool $response |
| | 993 | */ |
| | 994 | protected function get_plugin_dot_org_api_data( $slug ) { |
| | 995 | $response = get_site_transient( 'php_check-' . $slug ); |
| | 996 | if ( ! $response ) { |
| | 997 | $url = 'https://api.wordpress.org/plugins/info/1.2/'; |
| | 998 | $url = add_query_arg( array( |
| | 999 | 'action' => 'plugin_information', |
| | 1000 | 'request[slug]' => $slug, |
| | 1001 | ), $url ); |
| | 1002 | $response = wp_remote_get( $url ); |
| | 1003 | $response = null !== $response['body'] ? json_decode( $response['body'] ) : false; |
| | 1004 | |
| | 1005 | // Plugins not in dot org. |
| | 1006 | if ( ! isset( $response->requires_php ) ) { |
| | 1007 | $response->requires_php = false; |
| | 1008 | } |
| | 1009 | set_site_transient( 'php_check-' . $slug, $response ); |
| | 1010 | } |
| | 1011 | |
| | 1012 | return $response; |
| | 1013 | } |
| | 1014 | |