| | 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, 'remove_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 remove_plugin_update_row( $plugin_file, $plugin_data ) { |
| | 936 | print( '<script>' ); |
| | 937 | print( 'jQuery(".update[data-plugin=\'' . $plugin_file . '\']").removeClass("update");' ); |
| | 938 | print( 'jQuery("tr#' . $plugin_data['slug'] . '-update").remove();' ); |
| | 939 | print( '</script>' ); |
| | 940 | } |
| | 941 | |
| | 942 | /** |
| | 943 | * Unset update_plugins from update-core.php as appropriate. |
| | 944 | * |
| | 945 | * @param array $plugins Plugins for updating. |
| | 946 | * |
| | 947 | * @return array $plugins Plugins for updating. |
| | 948 | */ |
| | 949 | public function unset_plugin_updates( $plugins ) { |
| | 950 | if ( ! empty( $plugins ) ) { |
| | 951 | foreach ( $plugins as $file => $class ) { |
| | 952 | if ( $this->is_required_php( $class->update->slug ) ) { |
| | 953 | unset( $plugins[ $file ] ); |
| | 954 | $this->requires_php_update_notice(); |
| | 955 | } |
| | 956 | } |
| | 957 | } |
| | 958 | |
| | 959 | return $plugins; |
| | 960 | } |
| | 961 | |
| | 962 | /** |
| | 963 | * Create requires PHP update notice. |
| | 964 | */ |
| | 965 | public function requires_php_update_notice() { |
| | 966 | $message = '<span style="color:#f00;" class="dashicons dashicons-warning"></span> '; |
| | 967 | $message .= __( 'Some updates are not shown in this list because they require a newer version of PHP.' ); |
| | 968 | print( '<div class="notice-error notice"><p>' . $message . '</p></div>' ); |
| | 969 | } |
| | 970 | |
| | 971 | /** |
| | 972 | * Test for required PHP version. |
| | 973 | * |
| | 974 | * @param string $slug Slug of the repository being tested. |
| | 975 | * |
| | 976 | * @return bool True for below required PHP version. |
| | 977 | * False for above required PHP version or none set. |
| | 978 | */ |
| | 979 | protected function is_required_php( $slug ) { |
| | 980 | $response = $this->get_plugin_dot_org_api_data( $slug ); |
| | 981 | |
| | 982 | if ( ! $response->requires_php ) { |
| | 983 | return false; |
| | 984 | } |
| | 985 | |
| | 986 | return version_compare( PHP_VERSION, $response->requires_php, '<=' ); |
| | 987 | } |
| | 988 | |
| | 989 | /** |
| | 990 | * Get the dot org API data for the plugin or theme slug. |
| | 991 | * |
| | 992 | * @param string $slug Plugin or theme slug. |
| | 993 | * |
| | 994 | * @return object|bool $response |
| | 995 | */ |
| | 996 | protected function get_plugin_dot_org_api_data( $slug ) { |
| | 997 | $response = get_site_transient( 'php_check-' . $slug ); |
| | 998 | if ( ! $response ) { |
| | 999 | $url = 'https://api.wordpress.org/plugins/info/1.2/'; |
| | 1000 | $url = add_query_arg( array( |
| | 1001 | 'action' => 'plugin_information', |
| | 1002 | 'request[slug]' => $slug, |
| | 1003 | ), $url ); |
| | 1004 | $response = wp_remote_get( $url ); |
| | 1005 | $response = null !== $response['body'] ? json_decode( $response['body'] ) : false; |
| | 1006 | |
| | 1007 | // Plugins not in dot org. |
| | 1008 | if ( ! isset( $response->requires_php ) ) { |
| | 1009 | $response->requires_php = false; |
| | 1010 | } |
| | 1011 | set_site_transient( 'php_check-' . $slug, $response ); |
| | 1012 | } |
| | 1013 | |
| | 1014 | return $response; |
| | 1015 | } |
| | 1016 | |